Skip to content

Commit 85a2510

Browse files
committed
vue custom component&router
1 parent e675b3a commit 85a2510

5 files changed

Lines changed: 67 additions & 2 deletions

File tree

demo/vue_demo/myvue/src/App.vue

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,28 @@
22
<div id="app">
33
<img src="./assets/logo.png">
44
<router-view/>
5-
<hello-world/>
65
</div>
76
</template>
87

98
<script>
109
import HelloWorld from './components/HelloWorld.vue'
10+
import myHello from './components/hello.vue'
11+
12+
/*
13+
添加组件的流程:
14+
1. 在对应目录下新建组件,如 components/hello.vue
15+
2. 在 APP.vue 中引入组件,并且 export 出去
16+
3. 在 router/index.js 中引入组件,为组件添加路由信息
17+
18+
===
19+
工作流程:
20+
main.js 中从 APP.vue 引入了 name 为 'App' 的组件并且注册了 router 组件,并把内容返回到了 index.heml 中 id="app" 的 div 里面,APP.vue 中模板部分使用 router-view 注册了所有的 url,这样前端可以通过不同的 url 进行页面的跳转。
21+
22+
main.js 和 index.html 一般情况下是不用做改动的。
23+
*/
1124
1225
export default {
13-
components: { HelloWorld },
26+
components: { HelloWorld, myHello },
1427
name: 'App'
1528
}
1629
</script>

demo/vue_demo/myvue/src/components/HelloWorld.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
</template>
8585

8686
<script>
87+
// 对外暴露接口,接口里面的变量在自己的作用域里面有效
8788
export default {
8889
name: 'HelloWorld',
8990
data () {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<template>
2+
<div>
3+
<h2>页面不存在...</h2>
4+
</div>
5+
</template>
6+
7+
<script>
8+
export default {
9+
name: 'NotFound',
10+
// 路由钩子,添加进入路由前的动作,可以进行操作。例如获取数据、拦截操作等
11+
beforeRouteEnter (to, from, next) {
12+
next()
13+
},
14+
methods: {
15+
getData: function () {}
16+
}
17+
}
18+
</script>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<template>
2+
<div>
3+
自定义的 hello 页面。<br>
4+
你好,{{ name }}。
5+
</div>
6+
</template>
7+
8+
<script>
9+
export default {
10+
name: 'myHello',
11+
data () {
12+
return {
13+
name: 'Demo'
14+
}
15+
}
16+
}
17+
</script>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
11
import Vue from 'vue'
22
import Router from 'vue-router'
33
import HelloWorld from '@/components/HelloWorld'
4+
import myHello from '@/components/hello'
5+
import NotFound from '@/components/NotFound'
46

57
Vue.use(Router)
68

9+
/* export default命令,为模块指定默认输出。 export default 命令用于指定模块的默认输出。显然,一个模块只能有一个默认输出,因此export default命令只能使用一次。所以,import命令后面才不用加大括号,因为只可能唯一对应export default命令。 */
710
export default new Router({
811
routes: [
912
{
1013
path: '/',
1114
name: 'HelloWorld',
1215
component: HelloWorld
16+
},
17+
{
18+
path: '/hello',
19+
name: 'my-hello',
20+
component: myHello
21+
},
22+
{
23+
path: '/goHome',
24+
redirect: '/'
25+
},
26+
{
27+
path: '*',
28+
component: NotFound
1329
}
1430
]
1531
})

0 commit comments

Comments
 (0)