vue之组件的使用(router-view和components)&vue组件的复用&vue顶部导航栏实例
2023-09-14 09:04:07 时间
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
相关文章
- Android 开发之旅:深入分析布局文件&又是“Hello World!”
- [Vue @Component] Pass Props Between Components with Vue Slot Scope & renderless component
- [Angular] Isolate Scop &
- Apache Spark源码走读(十)ShuffleMapTask计算结果的保存与读取 &WEB UI和Metrics初始化及数据更新过程分析
- [Cloud Architect] 4. Introduction to Design for Cost, Performance, & Scalability
- [AngularJS] $scope & controllerAs
- [RSpec] LEVEL 2 CONFIGURATION & MATCHERS
- 【数组&双指针】LeetCode 76. 最小覆盖子串【困难】
- vue npm run serve/dev命令后台运行:nohup npm run serve >web.log 2>&1 & exit
- 容器监控—阿里云&容器内部服务监控
- C++中的Mat, const Mat, Mat &,Mat &, const Mat &的区别
- 使用nodejs&webpack&vue-cli脚手架工具搭建项目
- “华数杯”建模学习思考(Matlab&Python代码实现)
- HDU 5280 Senior's Array 最大区间和
- 事件监听 & 页面滚动(页面滚动到某一位置时显示/隐藏某元素,Vue环境)
- Chapter5 ROS常用组件(Ⅱ)----rosbag&rqt工具箱