Vuejs 路由器无法访问任何组件
Vuejs router cant reach any components
我可能使用了一些过时的指南,这可能是 Vuejs 新手的结果。
我的App.vue:
<template>
<div id="app">
<img src="../assets/logo.png">
<router-view/>
</div>
</template>
<script>
export default {
name: 'App'
}
</script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
我的main.js:
import Vue from 'vue'
import { App } from './app'
import router from './router'
import store from './store'
/* eslint-disable no-new */
new Vue({
el: '#app',
store,
router,
template: '<App/>',
components: { App }
})
router/index.js
import Vue from 'vue'
import Router from 'vue-router'
import { routes } from '../app'
Vue.use(Router)
export default new Router({
mode: 'history',
routes: routes
})
然后是app/routes.js
import accounts from './accounts'
import budgets from './budgets'
import transactions from './transactions'
export default [...accounts, ...budgets, ...transactions]
最后,app/accounts/routes.js
import * as components from './components'
export default [
{
path: '/',
component: components.AccountsListView,
name: 'accountsListView'
},
{
path: '/accounts/create',
component: components.CreateEditAccounts,
name: 'createAccounts'
},
{
path: '/accounts/edit',
component: componenets.CreateEditAccounts,
name: 'editAccounts'
}
]
出于某种原因,我无法点击我的任何组件,例如,如果我转到 localhost:8080/accounts/create
,那么我最终会在索引页面上更改 url 而不是点击相应的组件.我做的事情有什么明显的错误吗?
main.js:
import Vue from 'vue'
import { App } from './app'
import router from './router'
import store from './store'
/* eslint-disable no-new */
new Vue({
el: '#app',
store,
router,
render: h => h(App)
})
也许您正在使用 Vue 的仅运行时构建,其中模板编译器不可用。
编辑#1
App.vue
<template>
<div class="App">
<router-view />
</div>
</template>
那么您可能还没有在 App
组件中包含 router-view
。
对于这次出口
export default [
{
path: '/',
component: components.AccountsListView,
name: 'accountsListView'
},
{
path: '/accounts/create',
component: components.CreateEditAccounts,
name: 'createAccounts'
},
{
path: '/accounts/edit',
component: componenets.CreateEditAccounts,
name: 'editAccounts'
}
]
由于路由数组是默认导出,因此您需要使用此导入。
对象样式导入适用于有多个导出的情况。
import routes from '../app'
我可能使用了一些过时的指南,这可能是 Vuejs 新手的结果。
我的App.vue:
<template>
<div id="app">
<img src="../assets/logo.png">
<router-view/>
</div>
</template>
<script>
export default {
name: 'App'
}
</script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
我的main.js:
import Vue from 'vue'
import { App } from './app'
import router from './router'
import store from './store'
/* eslint-disable no-new */
new Vue({
el: '#app',
store,
router,
template: '<App/>',
components: { App }
})
router/index.js
import Vue from 'vue'
import Router from 'vue-router'
import { routes } from '../app'
Vue.use(Router)
export default new Router({
mode: 'history',
routes: routes
})
然后是app/routes.js
import accounts from './accounts'
import budgets from './budgets'
import transactions from './transactions'
export default [...accounts, ...budgets, ...transactions]
最后,app/accounts/routes.js
import * as components from './components'
export default [
{
path: '/',
component: components.AccountsListView,
name: 'accountsListView'
},
{
path: '/accounts/create',
component: components.CreateEditAccounts,
name: 'createAccounts'
},
{
path: '/accounts/edit',
component: componenets.CreateEditAccounts,
name: 'editAccounts'
}
]
出于某种原因,我无法点击我的任何组件,例如,如果我转到 localhost:8080/accounts/create
,那么我最终会在索引页面上更改 url 而不是点击相应的组件.我做的事情有什么明显的错误吗?
main.js:
import Vue from 'vue'
import { App } from './app'
import router from './router'
import store from './store'
/* eslint-disable no-new */
new Vue({
el: '#app',
store,
router,
render: h => h(App)
})
也许您正在使用 Vue 的仅运行时构建,其中模板编译器不可用。
编辑#1
App.vue
<template>
<div class="App">
<router-view />
</div>
</template>
那么您可能还没有在 App
组件中包含 router-view
。
对于这次出口
export default [
{
path: '/',
component: components.AccountsListView,
name: 'accountsListView'
},
{
path: '/accounts/create',
component: components.CreateEditAccounts,
name: 'createAccounts'
},
{
path: '/accounts/edit',
component: componenets.CreateEditAccounts,
name: 'editAccounts'
}
]
由于路由数组是默认导出,因此您需要使用此导入。
对象样式导入适用于有多个导出的情况。
import routes from '../app'