您的位置:首页 > 科技 > IT业 > 个人养老金制度有望年内_深圳网站建设网站优化服务_株洲企业seo优化_seo外链工具

个人养老金制度有望年内_深圳网站建设网站优化服务_株洲企业seo优化_seo外链工具

2025/3/28 7:31:51 来源:https://blog.csdn.net/2301_76272808/article/details/146407545  浏览:    关键词:个人养老金制度有望年内_深圳网站建设网站优化服务_株洲企业seo优化_seo外链工具
个人养老金制度有望年内_深圳网站建设网站优化服务_株洲企业seo优化_seo外链工具

路由的作用是根据URL来匹配对应的组件,并且无刷新切换模板的内容。vue.js中,可使用Vue Router来管理路由,让构建单页应用更加简单。

一、效果

二、实现

1.项目中安装Vue Router插件

pnpm install vue-router@lastest

2.main.js

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'const app = createApp(App);
app.use(router)
app.mount('#app')

3.   scr/views/Home.vue

<template><div id="app"><h1>hello vite</h1></div>
</template>

    src/views/About.vue

<template><div id="app"><h1>hello vite about</h1></div>
</template>

       src/views/news.vue

<template><div id="app"><h1>hello vite news</h1></div>
</template>

4.src/router/index.js:

import { createRouter, createWebHistory } from 'vue-router'import Home from '../views/Home.vue'
import News from '../views/News.vue'
import About from '../views/About.vue'const router = createRouter({history: createWebHistory(),routes: [{path: '/',name: '首页',redirect: '/home'},{path: '/home',name: '首页',component: Home},{path: '/news',name: '新闻',component: News},{path: '/about',name: 'Contact',component: About}]
})export default router​

5.scr/app.vue

<template><h1 class="title">Vue路由测试</h1><div class="container"><RouterLink to="/home"  class="item"  >首页</RouterLink><RouterLink to="/home"  class="item">产品</RouterLink><RouterLink :to="{ path: '/news' }" class="item" >新闻</RouterLink><RouterLink :to="{ name: 'Contact' }" class="item" >联系我们</RouterLink></div><div class="routerbox"><RouterView></RouterView></div>
</template><script  setup >
import { RouterLink, RouterView } from 'vue-router'
import { ref, reactive, watch, onMounted } from 'vue';
</script><style  scoped>
h1 {text-align: center;
}
.container {margin: 0 auto;width: 70%;display: flex;justify-content: space-evenly; 
} .container>.item {flex: 0 0 auto;background-color: #df6565;color: #fff;text-decoration: none;width: 100px;height: 50px;border-radius: 10%;text-align: center;line-height: 50px;font-size: 20px;}.container>.item:hover {background-color: #bec9c2;color: #ECC980;} 
.routerbox {margin: 0 auto;margin-top: 20px;width: 60%;height: 200px;border: 1px solid #000;border-radius: 10px;padding: 20px;
} 
</style>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com