1、nuxt的安装
nuxt官网链接地址
npx create- nuxt- app app
npm i
cd app
yarn run dev
2、了解nuxt的生命周期
fetch和asyncData在服务端和客户端是都有运行的,只不过运行过程中所处的环境问题,服务端中是没有windows对象存在的。
export default {
asyncData ( ) {
console. log ( window)
} ,
fetch ( ) {
console. log ( window)
} ,
created ( ) {
console. log ( window)
} ,
mounted ( ) {
console. log ( window)
}
}
3、解决seo问题的关键文件
下面代码中的head,就是方便浏览器抓取和了解内容
const pkg = require ( "./package" ) ;
module. exports = {
mode: "universal" ,
head: {
title: pkg. name,
meta: [
{ charset: "utf-8" } ,
{ name: "viewport" , content: "width=device-width, initial-scale=1" } ,
{ hid: "description" , name: "description" , content: pkg. description }
] ,
link: [ { rel: "icon" , type: "image/x-icon" , href: "/favicon.ico" } ]
} ,
loading: { color: "#fff" } ,
css: [ "element-ui/lib/theme-chalk/index.css" ] ,
plugins: [ "@/plugins/element-ui" , "@/plugins/router" ] ,
modules: [ "@nuxtjs/axios" ] ,
axios: {
proxy: true
} ,
proxy: {
"/api/" : "http://localhost:3001/"
} ,
build: {
transpile: [ /^element-ui/ ] ,
extend ( config, ctx) { }
}
} ;
文件中所引用的插件都会在/plugins目录下,这里引用element-ui插件;
import Vue from 'vue'
import Element from 'element-ui'
import locale from 'element-ui/lib/locale/lang/en'
export default ( ) => {
Vue. use ( Element, { locale } )
}
export default ( { app} ) => {
app. router. beforeEach ( ( to, from , next) => {
console. log ( '我要去:' + to. path) ;
next ( ) ;
} )
} ;
modules: [
'@nuxtjs/axios' ,
'@nuxtjs/router'
] ,
axios: {
proxy: true
} ,
proxy: {
'/api' : {
target: 'http://119.3.166.247:8080/' ,
changeOrigin: true ,
pathRewrite: {
'^/api' : '/'
}
}
} ,
export default {
fetch ( { app } ) {
console. log ( app. $axios)
} ,
asyncData ( { app } ) {
console. log ( app. $axios)
} ,
created ( ) {
console. log ( this . $axios)
}
}
export default function ( app) {
let axios = app. $axios;
axios. defaults. timeout = 10000
axios. defaults. headers. post[ 'Content-Type' ] = 'application/x-www-form-urlencoded'
axios. onRequest ( config => { } )
axios. onResponse ( res => { } )
axios. onError ( error => { } )
}