admin管理员组文章数量:1300084
I am trying to work on vue-router. But, I am stuck with a problem and I cannot find the solution after viewing many articles and docs.
I have already read couple of articles on how to use vue-router but they are not working.
This is my code.
I have made three ponents Home
Login
HelloWorld
I have made three routes.
Here is the code
main.js:
import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router';
// import Home from './ponents/Home.vue';
import HelloWorld from './ponents/HelloWorld.vue';
import Login from './ponents/Login.vue';
Vue.use(VueRouter)
Vue.config.productionTip = false
const router = new VueRouter({
mode: 'history',
base: __dirname,
routes: [
{ path: '/hello', ponent: HelloWorld },
{ path: '/login', ponent: Login },
{ path: '/', ponent: App }
]
});
new Vue({
router,
render: h => h(App),
}).$mount('#app')
App.vue:
<template>
<div id="app">
<HelloWorld/>
<Login />
</div>
</template>
<script>
import HelloWorld from './ponents/HelloWorld.vue'
import Login from './ponents/Login.vue'
export default {
name: 'app',
ponents: {
HelloWorld,
Login
}
}
</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>
Login.vue:
<template>
<div>
<h1>Login </h1>
</div>
</template>
<script>
export default {
name: 'Login',
}
</script>
<!-- Add "scoped" attribute to limit CSS to this ponent only -->
<style scoped>
</style>
HelloWorld.vue:
<template>
<div>
<h1>Hello World </h1>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
}
</script>
<!-- Add "scoped" attribute to limit CSS to this ponent only -->
<style scoped>
</style>
package.json
"name": "test-vue-router",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
},
"dependencies": {
"core-js": "^2.6.5",
"vue": "^2.6.10",
"vue-router": "^3.0.6"
},
"devDependencies": {
"@vue/cli-plugin-babel": "^3.8.0",
"@vue/cli-plugin-eslint": "^3.8.0",
"@vue/cli-service": "^3.8.0",
"babel-eslint": "^10.0.1",
"eslint": "^5.16.0",
"eslint-plugin-vue": "^5.0.0",
"vue-template-piler": "^2.6.10"
},
"eslintConfig": {
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/essential",
"eslint:remended"
],
"rules": {},
"parserOptions": {
"parser": "babel-eslint"
}
},
"postcss": {
"plugins": {
"autoprefixer": {}
}
},
"browserslist": [
"> 1%",
"last 2 versions"
]
}
Neither of the routes are working.
Everytime, I press enter by changing URL. the base route (i.e /
) always shows up.
And it shows App.vue poenent.
Thanks!
I am trying to work on vue-router. But, I am stuck with a problem and I cannot find the solution after viewing many articles and docs.
I have already read couple of articles on how to use vue-router but they are not working.
This is my code.
I have made three ponents Home
Login
HelloWorld
I have made three routes.
Here is the code
main.js:
import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router';
// import Home from './ponents/Home.vue';
import HelloWorld from './ponents/HelloWorld.vue';
import Login from './ponents/Login.vue';
Vue.use(VueRouter)
Vue.config.productionTip = false
const router = new VueRouter({
mode: 'history',
base: __dirname,
routes: [
{ path: '/hello', ponent: HelloWorld },
{ path: '/login', ponent: Login },
{ path: '/', ponent: App }
]
});
new Vue({
router,
render: h => h(App),
}).$mount('#app')
App.vue:
<template>
<div id="app">
<HelloWorld/>
<Login />
</div>
</template>
<script>
import HelloWorld from './ponents/HelloWorld.vue'
import Login from './ponents/Login.vue'
export default {
name: 'app',
ponents: {
HelloWorld,
Login
}
}
</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>
Login.vue:
<template>
<div>
<h1>Login </h1>
</div>
</template>
<script>
export default {
name: 'Login',
}
</script>
<!-- Add "scoped" attribute to limit CSS to this ponent only -->
<style scoped>
</style>
HelloWorld.vue:
<template>
<div>
<h1>Hello World </h1>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
}
</script>
<!-- Add "scoped" attribute to limit CSS to this ponent only -->
<style scoped>
</style>
package.json
"name": "test-vue-router",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
},
"dependencies": {
"core-js": "^2.6.5",
"vue": "^2.6.10",
"vue-router": "^3.0.6"
},
"devDependencies": {
"@vue/cli-plugin-babel": "^3.8.0",
"@vue/cli-plugin-eslint": "^3.8.0",
"@vue/cli-service": "^3.8.0",
"babel-eslint": "^10.0.1",
"eslint": "^5.16.0",
"eslint-plugin-vue": "^5.0.0",
"vue-template-piler": "^2.6.10"
},
"eslintConfig": {
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/essential",
"eslint:remended"
],
"rules": {},
"parserOptions": {
"parser": "babel-eslint"
}
},
"postcss": {
"plugins": {
"autoprefixer": {}
}
},
"browserslist": [
"> 1%",
"last 2 versions"
]
}
Neither of the routes are working.
Everytime, I press enter by changing URL. the base route (i.e /
) always shows up.
And it shows App.vue poenent.
Thanks!
Share Improve this question asked Jun 5, 2019 at 12:54 abhigyan nayakabhigyan nayak 1,4448 gold badges26 silver badges38 bronze badges 1- Try putting your current third route (/) as the first one. – Tim VN Commented Jun 5, 2019 at 13:10
2 Answers
Reset to default 5Your template inside app.vue
is missing router's ponent <router-view/>
,
Try this:
<template>
<div id="app">
<router-view/>
</div>
</template>
Note that the <router-view/>
tag is the place where you pages/ponents will be rendered.
Also the base route /
on your routes array is incorrect, the App
ponent is the root ponent and it will be holding other pages/ponents as its childs so App
ponent cannot be one of the routes.
It seem like your just missing your <router-view/>
tag on App.vue. <router-view/>
will render your ponents depending on the url so you don't need to import the ponents yourself.
Try this out:
<template>
<div id="app">
<router-view/>
</div>
</template>
本文标签: javascriptVuerouter only showing the base routeStack Overflow
版权声明:本文标题:javascript - Vue-router only showing the base route - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741651877a2390525.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论