Node.js/nuxt/asyncData:修订间差异
来自康健生活
| (未显示同一用户的10个中间版本) | |||
| 第1行: | 第1行: | ||
== 引用 == | == 引用 == | ||
asyncData 参数只在页面组件中有效。 | asyncData 参数只在页面组件中有效。 | ||
且第一次加载是服务端渲染,当在页面中再一次使用路由跳转到当前页面时(更换路由参数),则 HTTP 交互为浏览器客户端渲染 | |||
== ctx == | |||
<syntaxhighlight lang="javascript"> | |||
asyncData (ctx) { | |||
// todo | |||
},</syntaxhighlight> | |||
{| class="wikitable" | |||
|- | |||
| | |||
* $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: "<nowiki>http://static.askgo.cn/resource/intro/img/093fb01727e34dbaa230882edfbf4f1e.png</nowiki>"} | |||
* 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 == | ||
在页面组件中 this 的值为 undefined | 在页面组件中 this 的值为 undefined | ||