• element ui框架(路由)


    【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】

            有过web后端开发经验的同学,相信对路由这个概念应该不陌生。后端开发一般使用的是mvc,其中c,也就是controller,对应的就是各个路由的接口。现在整个前后端开发越来越独立化,两者之间只有数据的交互,没有界面的渲染工作,所以路由这部分工作也开始转到前端来完成。

            回到前端开发,当前已经有一个第三方库可以完美支持路由这个工作了,这就是vue-router。在用vue-cli创建项目的时候,一般也会提示我们是否需要安装router。

    1、创建一个带vue-router的项目rout

    vue init ./webpack rout

    2、首先确保node_modules下面vue-router已经被安装        

    1. cd node_modules
    2. dir vue*

    3、查看一下新代码系统为了支持vue-router做了哪些修改

    3.1 查看main.js

    1. // The Vue build version to load with the `import` command
    2. // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
    3. import Vue from 'vue'
    4. import App from './App'
    5. import router from './router'
    6. Vue.config.productionTip = false
    7. /* eslint-disable no-new */
    8. new Vue({
    9. el: '#app',
    10. router,
    11. components: { App },
    12. template: ''
    13. })

              通过阅读代码,可以发现有两个地方做了修改。第一,从./router中引入router,注意这里的router是自己编写的目录,不是安装的第三方库;第二,将router插入到Vue当中。

    3.2 查看App.vue

    1. <template>
    2. <div id="app">
    3. <img src="./assets/logo.png">
    4. <router-view/>
    5. </div>
    6. </template>
    7. <script>
    8. export default {
    9. name: 'App'
    10. }
    11. </script>
    12. <style>
    13. #app {
    14. font-family: 'Avenir', Helvetica, Arial, sans-serif;
    15. -webkit-font-smoothing: antialiased;
    16. -moz-osx-font-smoothing: grayscale;
    17. text-align: center;
    18. color: #2c3e50;
    19. margin-top: 60px;
    20. }
    21. </style>

              和之前相比较,最重要的修改就是添加了router-view,同时删除了HelloWorld。

    3.3 router/index.js

    1. import Vue from 'vue'
    2. import Router from 'vue-router'
    3. import HelloWorld from '@/components/HelloWorld'
    4. Vue.use(Router)
    5. export default new Router({
    6. routes: [
    7. {
    8. path: '/',
    9. name: 'HelloWorld',
    10. component: HelloWorld
    11. }
    12. ]
    13. }

            这部分是新增加的内容,之前的HelloWorld范例是没有的。通过观察发现,代码首先引入了Router,注意这个Router是来自于第三方库vue-router。其次,Vue中use了Router。接着,就是将HelloWorld放到了routes数组里面。至此,整个路由分析过程完毕。

    4、添加新的网页

            为了验证我们分析的思路是否正确,这里可以通过添加一个新网页的办法来实践确认一下。

    4.1 首先添加一个view目录,并且添加一个Content.vue

    1. <template>
    2. <div> 我是内容页</div>
    3. </template>
    4. <script>
    5. export default {
    6. name:'Content'
    7. }
    8. </script>
    9. <style scoped>
    10. </style>

    4.2 在router/index.js中添加路由,确保网页可以被访问到

    1. import Vue from 'vue'
    2. import Router from 'vue-router'
    3. import HelloWorld from '@/components/HelloWorld'
    4. import Content from '@/view/Content'
    5. Vue.use(Router)
    6. export default new Router({
    7. routes: [
    8. {
    9. path: '/',
    10. name: 'HelloWorld',
    11. component: HelloWorld
    12. },
    13. {
    14. path: '/Content',
    15. name: 'Content',
    16. component: Content
    17. }
    18. ]
    19. })

     4.3 除了添加网页和路由之外,剩下来的就是在App.vue中添加网页链接router-link

    1. <template>
    2. <div id="app">
    3. <img src="./assets/logo.png">
    4. <router-link to="/"> 首页 </router-link>
    5. <router-link to="/Content"> 内容 </router-link>
    6. <router-view/>
    7. </div>
    8. </template>
    9. <script>
    10. export default {
    11. name: 'App'
    12. }
    13. </script>
    14. <style>
    15. #app {
    16. font-family: 'Avenir', Helvetica, Arial, sans-serif;
    17. -webkit-font-smoothing: antialiased;
    18. -moz-osx-font-smoothing: grayscale;
    19. text-align: center;
    20. color: #2c3e50;
    21. margin-top: 60px;
    22. }
    23. </style>

    4.4 开始编译运行,如果之前已经编译好,可以忽略这一步

    npm run dev

    4.5 输入127.0.0.1:8082,你就可以看到这样的网页,

             注意,整个网页多了两个超链接,一个是首页,一个是内容。如果单击的首页,那么这部分内容不变。如果单击的是内容,就会看到这样的内容,

             至此,整个网页的跳转和编写就已经完成了。

  • 相关阅读:
    大规模语言模型人类反馈对齐--RLAIF
    流式 Isotype control 流式细胞仪control组
    SpingBoot的配置文件,SSM整合
    上帝视角一览大数据开发体系
    PHP8中的魔术方法-PHP8知识详解
    【数据结构】万字链表详解
    计算机毕业论文选题java毕业设计软件基于SSM实现的固定资产管理系统
    【FFmpeg实战】ffmpeg播放器-音视频解码流程
    Linux 小技巧1
    STM32一
  • 原文地址:https://blog.csdn.net/feixiaoxing/article/details/126914565