关于vue-router的使用注意

初次使用vue-router时,发现配置了routes数组仍然无法访问配置的path。

访问时会出现ConnotGET/XXXX

import Vue from 'vue';
import Router from 'vue-router';
import Home from 'src@/views/Home.vue'
import FlightList from 'src@/views/FlightList.vue'

Vue.use(Router)

export default new Router({
    mode: 'history',
    base: '/',
    routes: [
        {
            path: '/',
            name: 'Home',
            component: Home
        },
        {
            path: '/index',
            component: Home
        },
        {
            path: '/flight-list',
            name: 'flightList',
            component: FlightList
        }
    ]
})

经过google后发现,vue-route使用history模式下,需要服务器后端支持,也就是后端需要为对应的请求地址返回一个html地址。因此,应该在webpack.dev.config.js中为本地服务devServer配置一个:

devServer: {
        contentBase: './dist',
        hot: true,
        historyApiFallback: {
            rewrites: [
                { from: /^\*$/, to: '/dist/index.html' }
            ]
        }
    },

相关推荐