vue之组件的使用(router-view和components)&vue组件的复用&vue顶部导航栏实例

router-view的使用方法:

在vue里面是使用<router-link to="/" >来跳转路由的

1.在需要使用跳转的组件里,使用<router-link to="/" >

如:下图1

2.在需要使用跳转的组件里,使用 <router-view></router-view>

这样的话,路由匹配到的组件将渲染显示在这里

如:下图2

3.在index.js里面设置路由:

需要导入import然后写路由参数path,name,component等

如下图:

===============================================================================

components的使用方法:

1.新建组件TopNav.vue

<template>
    <div class="TopNav">
        <div>
            <router-link to="/">首页</router-link>
            <router-link to="/music">音乐</router-link>
            <router-link to="/itemBank">题库</router-link>
            <router-link to="/news">图片</router-link>
            <router-link to="/about">个人中心</router-link>
            <router-link to="login">登录</router-link>
        </div>
        <!--    路由出口-->
        <!--    路由匹配到的组件将渲染显示在这里-->
        <router-view></router-view>
    </div>
</template>

<script>
export default {
    name: 'TopNav',

}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
    .nav a {
        color: #42b983;
        margin: 0 10px;
    }
</style>

2.想在哪里复用就操作哪个文件,我这里是在App.vue里面复用这个组件:

关键步骤:

在script里面import

然后设置components

然后在template里面使用要复用的这个标签即可

App.vue

<template>
  <div id="app">
    <TopNav/>
  </div>
</template>

<script>
import './assets/css/my.css'
import TopNav from "./components/TopNav";
export default {
  name: 'App',
  components:{
    TopNav
  },
  methods: {

  }
}
</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>

完成!

参考:

vue之router-view组件的使用_luoyu6的博客-CSDN博客_router-view

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南北极之间

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值