File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22 <div id =" app" >
33 <img src =" ./assets/logo.png" >
44 <router-view />
5- <hello-world />
65 </div >
76</template >
87
98<script >
109import 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
1225export default {
13- components: { HelloWorld },
26+ components: { HelloWorld, myHello },
1427 name: ' App'
1528}
1629 </script >
Original file line number Diff line number Diff line change 8484</template >
8585
8686<script >
87+ // 对外暴露接口,接口里面的变量在自己的作用域里面有效
8788export default {
8889 name: ' HelloWorld' ,
8990 data () {
Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 11import Vue from 'vue'
22import Router from 'vue-router'
33import HelloWorld from '@/components/HelloWorld'
4+ import myHello from '@/components/hello'
5+ import NotFound from '@/components/NotFound'
46
57Vue . use ( Router )
68
9+ /* export default命令,为模块指定默认输出。 export default 命令用于指定模块的默认输出。显然,一个模块只能有一个默认输出,因此export default命令只能使用一次。所以,import命令后面才不用加大括号,因为只可能唯一对应export default命令。 */
710export 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} )
You can’t perform that action at this time.
0 commit comments