Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit51ff745

Browse files
author
kevin
committed
feat: add vue router
1 parent10f2973 commit51ff745

File tree

8 files changed

+141
-11
lines changed

8 files changed

+141
-11
lines changed

‎README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,50 @@ npx --no-install lint-staged
156156
```
157157

158158
###router
159+
160+
安装`vue-router`
161+
162+
```
163+
npm install vue-router@4
164+
```
165+
166+
创建`router/index.js`
167+
168+
```
169+
import { createRouter, createWebHashHistory } from 'vue-router'
170+
import Layout from '../layouts/Layout.vue'
171+
const routes = [
172+
{
173+
path: '/',
174+
name: 'Layout',
175+
component: Layout,
176+
redirect: 'home',
177+
children: [
178+
{
179+
path: 'home',
180+
name: 'Home',
181+
component: () => import('../pages/Home.vue')
182+
}
183+
]
184+
}
185+
]
186+
187+
const router = createRouter({
188+
routes: routes,
189+
history: createWebHashHistory()
190+
})
191+
192+
export default router
193+
```
194+
195+
`main.js`中引入路由
196+
197+
```
198+
...
199+
200+
import router from './router'
201+
202+
const app = createApp(App)
203+
app.use(router)
204+
app.mount('#app')
205+
```

‎package-lock.json

Lines changed: 34 additions & 1 deletion
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"prepare":"husky install"
1111
},
1212
"dependencies": {
13-
"vue":"^3.2.25"
13+
"vue":"^3.2.25",
14+
"vue-router":"^4.0.14"
1415
},
1516
"devDependencies": {
1617
"@babel/core":"^7.17.9",
@@ -34,4 +35,4 @@
3435
"prettier --write"
3536
]
3637
}
37-
}
38+
}

‎src/App.vue

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
<script setup>
2-
importHelloWorldfrom'./components/HelloWorld.vue'
3-
importMainfrom'./components/Main.vue'
4-
</script>
1+
<script setup></script>
52

63
<template>
7-
<imgalt="Vue logo"src="./assets/logo.png" />
8-
<HelloWorldmsg="Hello Vue 3 + Vite" />
9-
<Main />
4+
<router-view></router-view>
105
</template>
116

127
<style>

‎src/layouts/Layout.vue

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<template>
2+
<router-view></router-view>
3+
</template>
4+
5+
<script>
6+
exportdefault {
7+
setup() {
8+
return {}
9+
}
10+
}
11+
</script>
12+
13+
<style scoped></style>

‎src/main.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import{createApp}from'vue'
22
importAppfrom'./App.vue'
33

4-
createApp(App).mount('#app')
4+
importrouterfrom'./router'
5+
6+
constapp=createApp(App)
7+
app.use(router)
8+
app.mount('#app')

‎src/pages/Home.vue

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<template>
2+
<div>Home</div>
3+
</template>
4+
5+
<script>
6+
exportdefault {
7+
setup() {
8+
return {}
9+
}
10+
}
11+
</script>
12+
13+
<style scoped></style>

‎src/router/index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import{createRouter,createWebHashHistory}from'vue-router'
2+
importLayoutfrom'../layouts/Layout.vue'
3+
constroutes=[
4+
{
5+
path:'/',
6+
name:'Layout',
7+
component:Layout,
8+
redirect:'home',
9+
children:[
10+
{
11+
path:'home',
12+
name:'Home',
13+
component:()=>import('../pages/Home.vue')
14+
}
15+
]
16+
}
17+
]
18+
19+
constrouter=createRouter({
20+
routes:routes,
21+
history:createWebHashHistory()
22+
})
23+
24+
exportdefaultrouter

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp