Я новичок в Vue.js и знаю, что в маршруте есть несколько компонентов с разными именами.
Можно ли в файле App.vue изменить имя <router-view name = "default">
на другое? Спасибо за помощь.
HelloWorld.vue
<template>
<div class = "hello">
<h1>{{ msg }}</h1>
<h2>Essential Links</h2>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
font-weight: normal;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
</style>
App.vue
<template>
<div id = "app">
<!-- <img src = "./assets/logo.png"> -->
<router-view name = "default"></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>
index.js
import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
Vue.use(Router)
export default new Router({
routes: [
{
path: '/',
name: 'HelloWorld',
component: HelloWorld
}
]
})
Для этого вам понадобятся компоненты динамичный,
HTML код
<template>
<div id = "app">
<router-view :is = "currentComponent"></router-view>
</div>
</template>
Js-код: здесь в зависимости от значения isTrue
установите нужный вам компонент.
<script>
export default {
name: 'App' ,
components: {
LoginComponent,
HelloComponent
},
computed: {
currentComponent () {
return isTrue ? 'HelloComponent' : 'LoginComponent'
}
}
}
</script>
Хотел бы я, но router-view не поддерживает изменение тега имени github.com/vuejs/vue-router/issues/804&hl=en-IN
Спасибо. Но если я просто хочу статически изменить имя маршрутизатора. Есть какие-нибудь предложения сделать это?