日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > vue >内容正文

vue

Vue入门---- vue-router

發布時間:2025/3/15 vue 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Vue入门---- vue-router 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

#簡介:
vue-router官網
用 Vue.js + vue-router 可以快速創建SPA(單頁應用程序),是非常簡單的。使用 Vue.js ,我們已經可以通過組合Component來組成應用程序。
引入 vue-router 的過程:將組件(components)映射到路由(routes),然后告訴 vue-router 在哪里渲染它們。


##直接使用方法:

<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Document</title>// 注意順序不能顛倒!!!<script src="D:\Github\vue\lib\vue.js"></script><script src="D:\Github\vue\lib\vue-router.js"></script><script>window.onload = function() {// 1. 準備templatevar A = Vue.extend({template: "<h3>我是A</h3>"})var B = Vue.extend({template: "<h3>我是B</h3>"})// 2. 準備routesconst routes = [{path: "/A",component: A,}, {path: "/B",component: B,}, {path: '*',// 默認打開redirect:'/A'}]// 3. 調用vue-routerconst router = new VueRouter({//這里等價于routes: routes,不要隨意寫其他的名字!!!routes,})// 4. 掛載到vue上new Vue({router,el: '#box'})}</script> </head> <body><div id="box"><div><router-link to="/A">A</router-link><router-link to="/B">B</router-link> </div><div><router-view></router-view></div></div> </body> </html>

##模塊化使用方法

部分目錄結構如下:

├──node——modules ├──src├──assets├──components // 用來存放組件A.vue和B.vue│ ├──A.vue│ └──B.vue├──App.vue├──main.js├──router.config.js // router.config.js 用來存放路由信息└──...

一、組件定義

A.vue

<template><h3>我是A</h3> </template><script> export default {} </script><style></style>

B.vue

<template><h3>我是B</h3> </template><script> export default {} </script><style></style>

二、路由信息
router.config.js

import A from './components/A.vue' import B from './components/B.vue'export default {routes: [{path: "/A",component: A,}, {path: "/B",component: B,}, {path: "*",redirect: '/A'}] }

三、調用router并掛載到vue上
main.js

import Vue from 'vue' import VueRouter from 'vue-router'import App from './App.vue' import routes from './router.config.js'Vue.use(VueRouter);const router = new VueRouter(routes) new Vue({el: '#app',router,render: h => h(App) })

四、router-link與router-view
App.vue

<template><div id="app">{{msg}}<ul><li><router-link to='/A'>A</router-link><router-link to='/B'>B</router-link></li></ul><div><router-view></router-view></div></div> </template><script> export default {name: 'app',data () {return {msg: 'Welcome to Your Vue.js App'}} } </script><style></style>

總結

以上是生活随笔為你收集整理的Vue入门---- vue-router的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。