Vue 内置组件component

1

路由跳转<router-link>(或this.$router.push)与<router-view>在同一个vue里
vue是单页面应用,所有页面都在app.vue里
在app.vue里有<router-link><router-view>可以接受home组件
在home.vue里又有<router-link><router-view>

虽然叫路由跳转,在地址栏url也变化了,但其实没有跳到其他页面.
路由跳转是沿用以前多页面的叫法,在单页面应用中,其实是将其他页面加载到当前页面中,或者叫路由引用更为合适.

2 内置component

使用:is属性,动态渲染组件。
<component :is='state'></component>

示例:
<div id="app">
  <button @click="change1">son1</button>
  <button @click="change2">son2</button>
  <component :is='state'></component>
</div>
 
<script>
 var vm = new Vue({
    el: "#app",
    data() {
        return {
            state: 'son1'
        }
    },
    methods: {
        change1() {
            this.state = 'son1'
        },
        change2() {
            this.state = 'son2'
        }
    },
    components: {
        son1,
        son2
    }
 })
</script>
posted @ 2022-03-11 12:30  波吉国王  阅读(289)  评论(0编辑  收藏  举报