Vue学习之三 router 2 子路由

继续前面的写一个子路由

代码

  • 添加两个组件 HiVue1.vue HiVue2.vue
<template>
<div>
<h2> {{msg}} </h2>asfdasfasf1
</div>
</template>

<script>
export default {
  name: 'HI1',
  data () {
    return {
      msg: 'Welcome to Your Vue.js App Hi1'
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>

<template>
<div>
<h2> {{msg}} </h2>asfdasfasf2
</div>
</template>

<script>
export default {
  name: 'HI2',
  data () {
    return {
      msg: 'Welcome to Your Vue.js App Hi2'
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>

hiVue.vue中添加 router-view

<template>
<div>
<h2> {{msg}} </h2>asfdasfasf
  <router-view/>
</div>
</template>

<script>
export default {
  name: 'HI',
  data () {
    return {
      msg: 'Welcome to Your Vue.js App Hi'
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>

appVue中添加routerlinke

<template>
  <div id="app">
    <img src="./assets/logo.png">
    <div><router-link to="/hi">hi页面</router-link></div>
    <div><router-link to="/">首页</router-link></div>
    <div><router-link to="/hi/hi1">hi1页面</router-link></div>
    <div><router-link to="/hi/hi2">hi2页面</router-link></div>

    <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中添加子路由 (children)

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Hi from '@/components/HiVue'
import Hi1 from '@/components/HiVue1'
import Hi2 from '@/components/HiVue2'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/',
      name: 'HelloWorld',
      component: HelloWorld
    },
    {
      path: '/hi',
      name: 'hi',
      component: Hi ,
      children: [
        {
          path: '/',
          name: 'hi',
          component: Hi
        },
        {
          path: 'hi1',
          // name: 'hi1',
          component: Hi1
        },
        {
        path: 'hi2',
        // name: 'hi2',
        component: Hi2
        }
      ]
    }
  ]
})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值