Blogger Information
Blog 46
fans 2
comment 0
visits 19140
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
1、二级路由配置。 2、路由守卫。 3、组合API
P粉314265155
Original
639 people have browsed it

1、二级路由配置 #2、路由守卫

  1. router/index.js
  2. // hash模式:createWebHashHistory
  3. // import { createRouter, createWebHistory,createWebHashHistory } from 'vue-router'
  4. import { createRouter, createWebHistory } from 'vue-router'
  5. // 我们要想给页面 增加路由管理,就需要把页面引入进来
  6. // import 引入页面文件,也可以用方法来引入
  7. // / 代表根目录,根页面
  8. import HomeView from '../views/HomeView.vue'
  9. import Index from '../views/Index.vue'
  10. import User from '../views/User.vue'
  11. import Course from '../views/Course.vue'
  12. import Lists from '../views/Lists.vue'
  13. import Order from '../views/Order.vue'
  14. import Config from '../views/User/config.vue'
  15. // 注意大小写
  16. import Info from '../views/User/info.vue'
  17. const routes = [
  18. // {
  19. // path: '/',
  20. // name: 'home',
  21. // component: HomeView
  22. // },
  23. // {
  24. // path: '/about',
  25. // name: 'about',
  26. // route level code-splitting
  27. // this generates a separate chunk (about.[hash].js) for this route
  28. // which is lazy-loaded when the route is visited.
  29. // component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  30. // },
  31. {
  32. path: '/', // 路由地址配置,就是浏览器上的url
  33. alias : '/home', //起别名 多一个访问入口
  34. name: 'index', // 是页面的名称,自定义,但是不要重复
  35. component: Index, // 这个是页面文件的路径
  36. beforeEnter:(to, from) =>{ //独享的
  37. console.log(to);
  38. console.log(to,from);
  39. }
  40. },
  41. {
  42. path: '/lists', // 列表页面
  43. name: 'lists', // 是页面的名称,自定义,但是不要重复
  44. component: Lists // 这个是页面文件的路径
  45. },
  46. {
  47. path: '/order', // 列表页面
  48. name: 'order', // 是页面的名称,自定义,但是不要重复
  49. // component: Order // 这个是页面文件的路径
  50. redirect : "/lists" //页面重定向
  51. // redirect : (to) =>{
  52. // console.log(to);
  53. // }
  54. },
  55. {
  56. path: '/course', // 课程页面
  57. name: 'course', // 是页面的名称,自定义,但是不要重复
  58. component: Course // 这个是页面文件的路径
  59. },
  60. {
  61. path: '/user', // 用户页面
  62. name: 'user', // 是页面的名称,自定义,但是不要重复
  63. component: User, // 这个是页面文件的路径
  64. children :[ //配置子路由
  65. {
  66. // 实现路由二级
  67. path: 'info', // 用户页面
  68. name: 'info', // 是页面的名称,自定义,但是不要重复
  69. component: Info // 这个是页面文件的路径
  70. },
  71. {
  72. //实现了 页面二级
  73. path: '/config', // 用户页面
  74. name: 'config', // 是页面的名称,自定义,但是不要重复
  75. component: Config // 这个是页面文件的路径
  76. }
  77. ]
  78. },
  79. ]
  80. const router = createRouter({
  81. // hash模式:createWebHashHistory
  82. // 访问路径就会多个#号 localhost:8080/#/user、localhost:8080/#/course
  83. history: createWebHistory(process.env.BASE_URL),
  84. routes
  85. })
  86. // 导航守卫
  87. router.beforeEach ( (to,from)=>{
  88. if(to.path =='/user'){
  89. // 判断是否登录等
  90. // 主要用来通过跳转或取消的方式守卫导航
  91. // 有 前置守卫 和 后置钩子
  92. // 植入路由导航位置:全局导航、路由独享、组件级守卫
  93. alert('不能跳转');
  94. return false;
  95. }
  96. console.log(to); //到达页面
  97. console.log(from); //来源页面
  98. })
  99. // 导出路由
  100. export default router
  1. App.vue
  2. <template>
  3. <router-link to="/">我是首页</router-link>|
  4. <router-link to="/lists">我是列表</router-link>|
  5. <router-link to="/course?id=1&name=xiaomi">我是课程</router-link>|
  6. <router-link to="/user?id=1&name=xiaomi">我是用户</router-link>|
  7. <router-link to="/order">我是订单</router-link>|
  8. <br>
  9. <!-- <router-link to="/config">用户配置</router-link>|
  10. <router-link to="/info">用户信息</router-link>| -->
  11. <br><hr>
  12. {{url()}}
  13. <hr>
  14. <!-- Hash路由,带#号的,用a标签跳转的时候,要增加上#号 -->
  15. <a class="router-link-exact-active" href="/user">登录1 {{$route.path}}</a>
  16. <br>
  17. <!-- {} 是对象,里面是配置项, red 代表key,如果值等于true,就是red1 -->
  18. <a class="{red1:$route.path == '/user'}" href="/user">登录2 {{$route.path}}</a>
  19. <br>
  20. <a href="/user" :class="{red1:$route.path=='/user'}">登录 3{{$route.path}}</a>
  21. <br>
  22. <a href="user" :class="{red1:$route.path=='/user'}">登录 4{{$route.path}}</a>
  23. <br>
  24. <hr>
  25. {{url()}}
  26. <hr>
  27. <button @click="url1()">按钮跳转</button>
  28. <hr>
  29. <button @click="url1('/lists')">列表按钮</button>
  30. <br>
  31. <!-- js传值 -->
  32. <button @click="url2('/course',200)">课程按钮</button>
  33. <!-- app.vue页面没有刷新,刷新的是router-view 标签里的内容 -->
  34. <router-view></router-view>
  35. </template>
  36. <script>
  37. export default {
  38. methods :{
  39. url ( ) {
  40. // $route 路由全局变量,在js里,不能直接使用。要this.
  41. // $route 是url信息,路由页面信息
  42. console.log(this.$route);
  43. console.log(this.$router);
  44. },
  45. url1( url1) {
  46. // this.$router.push(url);push 可以跳转
  47. this.$router.push(url1);
  48. this.$router.push({
  49. path:url1,
  50. query:{id:id},
  51. });
  52. },
  53. url2( url2,id) {
  54. // this.$router.push(url);push 可以跳转
  55. this.$router.push(url2);
  56. this.$router.push({
  57. path:url2,
  58. query:{id:id},
  59. });
  60. },
  61. }
  62. }
  63. </script>
  64. <style >
  65. /* router-link标签会默认渲染为a标签
  66. 当router-link被点击时候的样式
  67. .router-link-exact-active
  68. router-link-active
  69. 区别
  70. https://blog.csdn.net/qq_41633597/article/details/109194275
  71. */
  72. a.router-link-exact-active,a.router-link-active {
  73. color: gold;
  74. }
  75. .red1{
  76. color: red;
  77. }
  78. </style>
  1. User.vue
  2. <template>
  3. <div>这是用户页面</div>
  4. <!-- 页面二级 -->
  5. <router-link to="/config">用户配置</router-link>|
  6. <!-- 路由二级 -->
  7. <router-link to="/user/info">用户信息</router-link>|
  8. <!-- app.vue页面没有刷新,刷新的是router-view 标签里的内容 -->
  9. <router-view></router-view>
  10. {{url()}}
  11. </template>
  12. <script>
  13. export default {
  14. methods :{
  15. url(){
  16. // console.log(this.$route);
  17. console.log(this.$route.query.id);
  18. console.log(this.$route.query.name);
  19. },
  20. }
  21. }
  22. </script>
  1. user/config.vue
  2. <template>
  3. <div>这是用户配置页面</div>
  4. </template>
  5. user/info.vue
  6. <template>
  7. <div>这是用户信息页面</div>
  8. </template>

3、组合API

  1. props:
  2. App.vue
  3. <template>
  4. <div>
  5. <Props></Props>
  6. <hr>
  7. <Props :titel="title"></Props>
  8. </div>
  9. </template>
  10. <script>
  11. import { ref } from 'vue'
  12. import Props from './components/Props';
  13. export default {
  14. components : {
  15. Props
  16. },
  17. setup(){
  18. const title = ref('百度');
  19. return {
  20. title
  21. }
  22. }
  23. }
  24. </script>
  25. components/Props.vue
  26. <template>
  27. <!-- 组件
  28. 组件Props它也是vue后缀名文件,它也有setup -->
  29. <li>你好</li>
  30. <li>再见</li>
  31. <div class="red">
  32. {{title}}
  33. </div>
  34. </template>
  35. <script>
  36. export default ({
  37. // 组件它也是vue后缀名文件,它也有setup
  38. // props 之前props接收参数,是跟setup、data同一级的
  39. // 有setup,就调用不到props里的数据了,因为不能用this
  40. props : {
  41. title:{
  42. type : String
  43. } },
  44. setup(props,context) {
  45. // setup 在组件里使用了,接收的 传值props,就在setup里不能使用
  46. // 所有想在setup使用传值,要在setup接收参数
  47. console.log(props);
  48. console.log(context);
  49. return {
  50. title : props.title
  51. }
  52. }
  53. })
  54. </script>
  1. 响应:reactive
  2. <template>
  3. {{data}}
  4. {{fun()}}
  5. <!-- <div @click="fun">{{data}}--{{num}}</div> -->
  6. <hr>
  7. <div>{{data.num}}</div>
  8. <hr>
  9. <button @click="fun">添加</button>
  10. </template>
  11. <!-- <script>
  12. export default {
  13. setup(props) {
  14. const data = '小牛';
  15. const fun = ()=>{
  16. alert('这是方法')
  17. }
  18. return {
  19. data,
  20. fun,
  21. }
  22. }
  23. }
  24. </script> -->
  25. <script >
  26. import {reactive} from 'vue';
  27. // 3.2 新增 的功能,组合api功能
  28. // 在 Script 标签上增加的setup参数
  29. // 新增加的不需要retrun返回了,更加像js代码了
  30. // 这两写法 更加推荐下面的写法
  31. export default {
  32. setup(){
  33. // setup里的数据不是响应式,是因为setup最早执行的,没有this
  34. const data = reactive ({
  35. num : 1
  36. })
  37. const fun =( ) =>{
  38. data.num = data.num+1;
  39. }
  40. return {
  41. data,
  42. fun,
  43. }
  44. }
  45. }
  46. </script>
  1. 插槽
  2. <template>
  3. <div>123</div>
  4. <hello>自定义标签</hello>
  5. <OneOne></OneOne>
  6. <!-- <two></two> -->
  7. <!-- 3组件传值 -->
  8. <OneOne title="热门课程"></OneOne>
  9. <!-- 4 动态绑定 增加冒号可以动态传值,值可以动态绑定 -->
  10. <OneOne :title="title"></OneOne>
  11. <!-- 4.2 多个传值 - 传值的 key {title arr}-> -->
  12. <OneOne :title="title" :arr="arr"></OneOne>
  13. <hr>
  14. <!-- 组件向页面传值 -->
  15. <!-- fun5 跟引入组件事件名称一致 -->
  16. <three @fun5 = "fun4"></three>
  17. <div>{{sc_sum}}</div>
  18. <hr>
  19. <!-- 多组件 多层相互访问 -->
  20. <!-- <four></four> -->
  21. <two>哈哈</two>
  22. <hr>
  23. <five>
  24. <a href="http://www.baidu.com">跳转页面</a>
  25. </five>
  26. <five>
  27. <img style="width:200px;" src="https://img1.baidu.com/it/u=2218815005,3124653454&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500" alt="">
  28. </five>
  29. <five>
  30. <template #img>
  31. <img src="https://img1.baidu.com/it/u=2204132179,643663242&fm=253&fmt=auto&app=120&f=JPEG?w=1200&h=800" alt="">
  32. </template>
  33. </five>
  34. <five>
  35. <!-- v-slot:img2 语法糖 #img2 -->
  36. <template #img2>
  37. <a href="http://www.baidu.com">你好</a>
  38. </template>
  39. </five>
  40. </template>
  41. <script>
  42. // 组件导入。哪个页面使用哪个页面导入
  43. // import from 格式
  44. // 名称 路径
  45. // import moduleName from 'module';
  46. // import HelloWorld from './components/HelloWorld.vue'
  47. import hello from "./components/HelloWorld";
  48. // import One from './components/One.vue';
  49. import OneOne from "./components/OneOne";
  50. import Two from "./components/Two.vue"
  51. // import Three from './components/Three.vue';
  52. // import two from './components/Two.vue';
  53. import three from "./components/Three.vue";
  54. // import Four from "./components/Four.vue";
  55. import Five from "./components/Five.vue";
  56. import Four from './components/Four.vue';
  57. export default {
  58. components: {
  59. // 后面的值是引入的文件
  60. // 前面的lkey是给这个文件下标的名称
  61. hello: hello,
  62. OneOne: OneOne,
  63. three:three,
  64. Two:Two,
  65. Five:Five,
  66. Four,
  67. // Four:Four,
  68. // two:two
  69. },
  70. data() {
  71. return {
  72. arr :{
  73. lh : "小蛇",
  74. hui : "小龙",
  75. },
  76. titel : "www.baidu.com",
  77. sc_sum:0,
  78. php:"百度",
  79. }
  80. },
  81. methods: {
  82. fun4(e){
  83. this.sc_sum =this.sc_sum+ 2;
  84. },
  85. fun3(){
  86. console.log("这是方法三");
  87. },
  88. fun5(){
  89. console.log("这是方法5");
  90. },
  91. },
  92. beforeCreate() {
  93. console.log("1在创建组件之前调用");
  94. },
  95. created() {
  96. console.log("2组件创建完成调用");
  97. },
  98. beforeMount() {
  99. console.log("3模版挂载之前调用");
  100. },
  101. mounted() {
  102. console.log("4模版挂载完成调用");
  103. },
  104. // name: 'App',
  105. // components: {
  106. // HelloWorld
  107. // }
  108. };
  109. </script>
  110. <style scoped >
  111. /* scoped 防止样式穿透 */
  112. div {
  113. color: aqua;
  114. }
  115. </style>
  1. 插槽scoped样式穿透
  2. <template>
  3. <router-link to="/">我是首页</router-link>|
  4. <router-link to="/lists">我是列表</router-link>|
  5. <router-link to="/course">我是课程</router-link>|
  6. <router-link to="/user">我是用户</router-link>|
  7. <br><hr>
  8. <!-- app.vue页面没有刷新,刷新的是router-view 标签里的内容 -->
  9. <router-view></router-view>
  10. <hr>
  11. <br>
  12. <br>
  13. <six v-slot:img="data">
  14. <div class="red">小马---{{data.php1}}---{{data.a}}---{{data}}</div>
  15. </six>
  16. <hr>
  17. <six #img>
  18. <div>小猴</div>
  19. </six>
  20. </template>
  21. <script>
  22. import Six from"./components/Six.vue"
  23. export default {
  24. components :{
  25. Six,
  26. }
  27. }
  28. </script>
  29. <style scoped>
  30. .red {
  31. color: red;
  32. }
  33. </style>
  1. 监听:watch
  2. <template>
  3. <div>{{data.num}}</div>
  4. <hr>
  5. <button @click="fun">添加</button>
  6. <br>
  7. {{str}}
  8. <br>
  9. {{str1}}
  10. <hr>
  11. <div>a:{{a}}---b:{{b}}</div>
  12. <button @click="add">按钮</button>
  13. <hr>
  14. <div>倒计时:{{b}}</div>
  15. </template>
  16. <script setup >
  17. // reactive,ref 响应 watch vue 下的监听模块
  18. import {reactive,ref, watch} from 'vue';
  19. // import {ref} from 'vue';
  20. const data = reactive ({
  21. num : 1
  22. })
  23. const fun =( ) =>{
  24. data.num = data.num+1;
  25. console.log(str);
  26. // console.log(str1);
  27. console.log(str1.value);
  28. }
  29. const str ='小伙';
  30. const str1 = ref('小孩');
  31. // js 跟php 执行顺序不一样
  32. const add = ()=>{
  33. a.value++;
  34. b.value--;
  35. }
  36. let a = ref(0);
  37. let b = ref(60);
  38. // watch ( ()=>{
  39. // console.log(a.value);
  40. // })
  41. watch (a,(new_a,old_a)=>{
  42. console.log(a.value);
  43. // 变量是为了获取改变的值,new_a,获取改变的值
  44. console.log(new_a);
  45. // 第二参数, 获取未改变之前的值
  46. console.log(old_a);
  47. });
  48. </script>
  1. 生命周期:onBeforeMount
  2. <template>
  3. <div>{{data.num}}</div>
  4. <hr>
  5. <button @click="fun">添加</button>
  6. <br>
  7. {{str}}
  8. <br>
  9. {{str1}}
  10. <hr>
  11. <div>a:{{a}}---b:{{b}}</div>
  12. <button @click="add">按钮</button>
  13. <hr>
  14. <div>倒计时:{{b}}</div>
  15. </template>
  16. <script setup >
  17. // reactive,ref 响应 watch vue 下的监听模块
  18. // 生命周期 使用前要引入 onBeforeMount 模版挂载之前调用
  19. import {reactive,ref, watch,onBeforeMount} from 'vue';
  20. onBeforeMount( ()=>{
  21. console.log('onBeforeMount');
  22. } )
  23. // import {ref} from 'vue';
  24. const data = reactive ({
  25. num : 1
  26. })
  27. const fun =( ) =>{
  28. data.num = data.num+1;
  29. console.log(str);
  30. // console.log(str1);
  31. console.log(str1.value);
  32. }
  33. const str ='小伙';
  34. const str1 = ref('小孩');
  35. // js 跟php 执行顺序不一样
  36. const add = ()=>{
  37. a.value++;
  38. b.value--;
  39. }
  40. let a = ref(0);
  41. let b = ref(60);
  42. // watch ( ()=>{
  43. // console.log(a.value);
  44. // })
  45. watch (a,(new_a,old_a)=>{
  46. console.log(a.value);
  47. // 变量是为了获取改变的值,new_a,获取改变的值
  48. console.log(new_a);
  49. // 第二参数, 获取未改变之前的值
  50. console.log(old_a);
  51. });
  52. </script>
  1. 引入路由
  2. <template>
  3. </template>
  4. <script >
  5. // 需要引入路由模块 vue-router
  6. import { useRoute, useRouter } from 'vue-router';
  7. // 获取页面的信息
  8. const route = useRoute();
  9. // 在setup里不能使用了
  10. // console.log(this.$route.path);
  11. console.log(route.path);
  12. // 页面跳转,传值
  13. const router = useRouter();
  14. </script>
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!