查看“︁Node.js/nuxt/asyncData”︁的源代码
来自康健生活
←
Node.js/nuxt/asyncData
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
== 引用 == asyncData 参数只在页面组件中有效。 === ctx === <syntaxhighlight lang="javascript"> asyncData (ctx) { // todo },</syntaxhighlight> {{leftrightflex| * $axios: (...) * $config: (...) * $cookies: (...) * $errorHandler: (...) * app: (...) * base: (...) * env: (...) * error: (...) * from: (...) * isDev: (...) * isHMR: (...) * isStatic: (...) * next: (...) * nuxtState: (...) * params: (...) * payload: (...) * query: (...) * redirect: (...) * route: (...) * store: (...) * _errored: (...) * _redirected: (...) | * $axios: ƒ wrap() * $config: {} * $cookies: {parseJSON: true, set: ƒ, setAll: ƒ, get: ƒ, getAll: ƒ, …} * $errorHandler: ƒ (err) * app: {head: {…}, store: Store, router: VueRouter, nuxt: {…}, render: ƒ, …} * base: "/" * env: {host: {…}, target: "development", SECRET: "kangkk"} * error: ƒ () * from: {name: "/down/detail", meta: Array(1), path: "/down/detail/22.html", hash: "#93", query: {…}, …} * isDev: true * isHMR: false * isStatic: false * next: ƒ () * nuxtState: {layout: "default", data: Array(1), fetch: Array(0), error: null, state: {…}, …} * params: {id: "22", bid: 0, title: "嘻嘻嘻", image: "http://static.askgo.cn/resource/intro/img/093fb01727e34dbaa230882edfbf4f1e.png"} * payload: undefined * query: {} * redirect: ƒ (status, path, query) * route: {name: "/pay/statement", meta: Array(1), path: "/pay/statement", hash: "", query: {…}, …} * store: Store {_committing: false, _actions: {…}, _actionSubscribers: Array(1), _mutations: {…}, _wrappedGetters: {…}, …} * _errored: true * _redirected: false * __proto__: Object }} ==== 详细 ==== ---> [[/asyncdata详细内容|asyncdata详细内容]] == data == 在 asyncData 中获取数据,return 的数据,nuxt 会自动混入到 data 中。 === 拿到数据 === <syntaxhighlight lang="javascript"> async asyncData ({ params, route, error }) { const [bar] = await Promise.all([ axios.get('https://api.***') ]) return { foo: bar } },</syntaxhighlight> <syntaxhighlight lang="javascript"> async asyncData() { let { data } = await axios.get('https://***') return { foo: data } }</syntaxhighlight> <syntaxhighlight lang="javascript"> asyncData() { return axios.get('https://api***') .then((res) => { return {foo: res.data} }) }</syntaxhighlight> {{Warning|这个时候在 data 中一定不要定义如上例中的 foo 参数,不然会拿不到数据}} <syntaxhighlight lang="javascript"> // 错误行为 data () { return { foo: [], } },</syntaxhighlight> == this == 在页面组件中 this 的值为 undefined
此页面嵌入的页面:
模板:Meta box
(
查看源代码
)
模板:Warning
(
查看源代码
)
模板:Warning/style.css
(
查看源代码
)
返回
Node.js/nuxt/asyncData
。
导航菜单
康健生活
搜索
user-page
通知
user-interface-preferences
个人工具
登录
五一
多读书,多运动,多到远处走走,再看看不一样的世界。