如何使用vue-router与vue 2.x版本



import Vue from ' Vue '从"Vuex"导入Vuex从vue-router中导入VueRouter导入"es6-promise/汽车">

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import App from './App.vue'
//import About from "./components/About.vue"
Vue.config.productionTip = false
Vue.use(ElementUI);
Vue.use(Vuex)
Vue.use(VueRouter)
const store = new Vuex.Store({ ... })
const routes = [
{ path: '/about', component: { template: '<div>foo</div>' } },
]
const router = VueRouter({
routes, // short for `routes: routes`
})
new Vue({
render: h => h(App),
router,
store,
}).$mount('#app')

我尝试使用router,但是下面给出err:

Uncaught TypeError: this is undefined
VueRouter vue-router.esm.js:2699
<anonymous> main.js:84
js app.js:1351
__webpack_require__ app.js:849
fn app.js:151
1 app.js:1364
__webpack_require__ app.js:849
checkDeferredModules app.js:46
<anonymous> app.js:925
<anonymous> app.js:928
vue-router.esm.js:2699
VueRouter vue-router.esm.js:2699
<anonim> main.js:84
js app.js:1351
__webpack_require__ app.js:849
fn app.js:151
1 app.js:1364
__webpack_require__ app.js:849
checkDeferredModules app.js:46
<anonim> app.js:925
<anonim> app.js:928

我创建了我的项目vue cli,然后我用npm -i安装vue-router,为什么它不这样工作,或者我应该添加vue add routercli命令?

以下是我的版本:

"vue": "^2.6.12",
"vue-router": "^3.2.0",
"vuex": "^3.6.2"
"@vue/cli-plugin-babel": "~4.5.0",
"@vue/cli-plugin-eslint": "~4.5.0",
"@vue/cli-plugin-router": "^4.5.11",
"@vue/cli-service": "~4.5.0",
"babel-eslint": "^10.1.0",
"babel-plugin-component": "^1.1.1",
"eslint": "^6.7.2",
"eslint-plugin-vue": "^6.2.2",
"vue-template-compiler": "^2.6.11"

该错误不是VueRouter导入错误,而是VueRouter使用错误。

VueRouter函数是获取路由器实例的构造函数。在你的代码中,你必须这样做来修复你的问题:

const router = new VueRouter({
routes, // short for `routes: routes`
})

相关内容

  • 没有找到相关文章