axios 添加公共參數(shù),比如從url上取參數(shù)
在plugins文件夾下面建個(gè)api.js,然后在nuxt-config.js配置
plugins: [{
src: '@/plugins/api',
}],
import axios from './axios'
import apiConfig from './api-config'
import utils from './utils'
import Vue from 'vue'
export default ({store, app}) => {
let methods = ['get','post','put','patch','delete'];
Vue.prototype.server=app.context.server={}
for (let method of methods) {
app.context.server[method]=Vue.prototype.server[method] = function (key, data={}, option={}) {
let argArr=[]
let commonData={
lang:store.state.lang
}
data=utils.extend(commonData,data)
if(!key){
throw new Error('\'key\' argument in server function don\'t have a value')
}else if(!apiConfig[key]){
throw new Error(key + ' not defined in \'api-config\' file')
}
if(method === 'get'){
option.params = data
argArr.push(apiConfig[key], option)
}else if(method === 'post' || method === 'put' || method === 'patch'){
argArr.push(apiConfig[key], data, option)
}
return axios[method].apply(null, argArr)
// return axios[method](apiConfig[key], data, option)
.then((response) => {
let resData = response.data
if (resData.code === 1) {
let msg = '系統(tǒng)異常'
// Vue.prototype.$confirm(msg, '錯(cuò)誤', {
// confirmButtonText: 'OK',
// showCancelButton: false,
// type: 'error'
// })
}
return resData
})
.catch((err) => {
// Vue.prototype.$confirm(err.config.url + ' ' + err.response.statusText, '錯(cuò)誤', {
// confirmButtonText: 'OK',
// showCancelButton: false,
// type: 'error'
// })
console.error(err)
return err
})
}
}
// console.log(app.context.server,6547)
}
完成后就可以在頁面中引用
```javascript
asyncData (context) {
return context.server.post('getStyleDetail',{"designId": 101971
}).then((res) => {
return { title: res.message }
})
},
mounted(){
this.server.post('getStyleDetail',{
"designId": 101971
}).then((res) => {
this.title=res.message
})
#### # [how to write global router-function in nuxt.js](https://stackoverflow.com/questions/45509472/how-to-write-global-router-function-in-nuxt-js)
You can create a plugin for Nuxt
create a `plugin/route.js` file:
export default ({ app }) => {
// Every time the route changes (fired on initialization too)
app.router.afterEach((to, from) => {
//do something to validate
}
}
and update your `nuxt.config.js` file:
plugins: ['~/plugins/route']
More details about Nuxt plugins: [https://nuxtjs.org/guide/plugins](https://nuxtjs.org/guide/plugins)
#### Cannot read property '_t' of undefined
```javascript
import Vue from 'vue'
import VueI18n from 'vue-i18n'
Vue.use(VueI18n)
export default ({ app, store }) => {
// Set i18n instance on app
// This way we can use it in middleware and pages asyncData/fetch
app.i18n = new VueI18n({
locale: store.state.locale,
fallbackLocale: 'en',
messages: {
'en': require('~/locales/en.json'),
'fr': require('~/locales/fr.json')
}
})
app.i18n.path = (link) => {
if (app.i18n.locale === app.i18n.fallbackLocale) {
return `/${link}`
}
return `/${app.i18n.locale}/${link}`
}
}
參考:https://zh.nuxtjs.org/examples/i18n#codefund_ad
http://kazupon.github.io/vue-i18n/guide/lazy-loading.html
多語言vue-i18n
nuxtjs [vue-i18n] Value of key 'message.hello' is not a string!
import {i18n,loadLanguageAsync} from '~/setup/i18n-setup'
export default function ({app}) {
app.router.beforeEach((to, from, next) => {
const urlLang = to.params.lang
let lang
if(urlLang === '1'){
lang='en'
}else if(urlLang === '0'){
lang='sa'
}
loadLanguageAsync(lang).then(() => next())
})
}
to.params.lang為undefined,改成to.query.lang,
url 為http://localhost:3000/test?lang=1,需要從to.query中取。
頁面添加公共樣式:
在 nuxt.config.js 中添加 CSS 資源:
Nuxt.js 讓你可以配置全局 CSS 文件、模塊、庫(每個(gè)頁面都會(huì)被引入)。
module.exports = {
css: [
// 加載一個(gè) node.js 模塊
'hover.css/css/hover-min.css',
// 同樣加載一個(gè) node.js 模塊,不過我們定義所需的預(yù)處理器
{ src: 'bulma', lang: 'sass' },
// 項(xiàng)目中的 CSS 文件
'~assets/css/main.css',
// 項(xiàng)目中的 Sass 文件
{ src: '~assets/css/main.scss', lang: 'scss' } // 指定 scss 而非 sass
]
}