ASCII码 ASCII码

事件绑定、组件、路由实例演示

发布于:2022-05-17 12:17:59  栏目:技术文档

1、v-on:绑定事件

  1. <template>
  2. <div>我是:{{ name }}</div>
  3. <div>
  4. <div>{{ name }} +{{ name2 }}</div>
  5. <div>{{ num1 + num2 }}</div>
  6. </div>
  7. <div>
  8. <!-- 1、v-on:绑定事件 -->
  9. <!-- 点击事件、键盘事件、回车键事件 -->
  10. <!-- 语法糖@ -->
  11. <button v-on:click="fun('liyufeng', 1)">按钮1</button>
  12. <button @click="fun('liuqiang', 2)">按钮2</button>
  13. </div>
  14. <div @click="one()">
  15. 第一层
  16. <div @click="two()">
  17. 第二层
  18. <div @click.stop="three()">第三层(.stop阻止冒泡)</div>
  19. </div>
  20. </div>
  21. <!-- 2、流程 :v-if , v-show-->
  22. <!-- 3、v-if , v-show 效果一样,代码显示不一样 -->
  23. <div>
  24. <button @click="fun1()">按钮:显示 or 隐藏</button>
  25. <div v-if="is">v-if:显示 or 隐藏</div>
  26. <div v-show="is">v-show:显示 or 隐藏</div>
  27. </div>
  28. <div>
  29. <!-- 4、v-else v-else-if -->
  30. <button @click="fun2()">按钮:显示 or 隐藏</button>
  31. <div v-if="is == 1">李磊</div>
  32. <div v-else-if="is == 2">黄晓明</div>
  33. <div v-else>韩梅梅</div>
  34. </div>
  35. <div>
  36. <!-- 5、循环:v-for -->
  37. <div v-for="item in yjz" v-bind:key="item">{{ item }}</div>
  38. </div>
  39. </template>
  40. <script>
  41. //JS代码
  42. export default {
  43. data() {
  44. return {
  45. name: "vue",
  46. name2: "php",
  47. num1: 20,
  48. num2: 10,
  49. is: false,
  50. yjz: ["李sir", "刘sir"],
  51. };
  52. },
  53. methods: {
  54. fun(e, ee) {
  55. console.log(e, ee);
  56. console.log("方法1");
  57. },
  58. // v-if
  59. fun1() {
  60. this.is = !this.is;
  61. },
  62. fun2() {
  63. if (this.is == 2) {
  64. this.is = 0;
  65. } else {
  66. this.is = this.is + 1;
  67. }
  68. },
  69. // 冒泡
  70. one() {
  71. console.log("第一层");
  72. },
  73. two() {
  74. console.log("第二层");
  75. },
  76. three() {
  77. console.log("第三层");
  78. },
  79. },
  80. };
  81. </script>
  82. <style></style>

2、组件演示

App.vue代码

  1. <template>
  2. <div>
  3. <!-- 6、组件 -->
  4. <OneComVue title="我是通过组件属性传的值"></OneComVue>
  5. <OneComVue :title="title"></OneComVue>
  6. <OneComVue :title="arr"></OneComVue>
  7. <OneComVue :title="obj"></OneComVue>
  8. <TwoComVue :title="title"></TwoComVue>
  9. </div>
  10. </template>
  11. <script>
  12. // 组件导入
  13. import OneComVue from "./components/OneCom.vue";
  14. import TwoComVue from "./components/TwoCom.vue";
  15. //JS代码
  16. export default {
  17. components: {
  18. OneComVue: OneComVue,
  19. // OneComVue,
  20. TwoComVue: TwoComVue,
  21. },
  22. data() {
  23. return {
  24. title: "我是通过OneComVue组件传的值",
  25. arr: ["数学", "语文", "英语"],
  26. obj: {
  27. shuxue: "数学",
  28. yuwen: "语文",
  29. yingyu: "英语",
  30. },
  31. };
  32. },
  33. };
  34. </script>
  35. <style></style>

OneCom.vue代码

  1. <template>
  2. <div>这是组件OneCom,通过属性传值:{{ title }}</div>
  3. </template>
  4. <script>
  5. export default {
  6. name: "OneCom",
  7. //属性传值,title是变量
  8. props: ["title"],
  9. };
  10. </script>

3、路由演示

App.vue

  1. <template>
  2. <div>
  3. <!-- router-link 路由跳转的标签,和a标签一样 -->
  4. <router-link to="/">首页</router-link> |
  5. <router-link to="/about">关于我们</router-link> |
  6. <router-link to="/user">会员中心</router-link>
  7. </div>
  8. <!-- router-view 路由的展示区 -->
  9. <router-view />
  10. </template>
  11. <script></script>
  12. <style></style>

index.js

  1. import { createRouter, createWebHistory } from "vue-router";
  2. import HomeView from "../views/HomeView.vue";
  3. import UserView from "../views/UserView.vue";
  4. const routes = [
  5. {
  6. path: "/",
  7. name: "home",
  8. component: HomeView,
  9. },
  10. {
  11. path: "/about",
  12. name: "about",
  13. // route level code-splitting
  14. // this generates a separate chunk (about.[hash].js) for this route
  15. // which is lazy-loaded when the route is visited.
  16. component: () =>
  17. import(/* webpackChunkName: "about" */ "../views/AboutView.vue"),
  18. },
  19. {
  20. path: "/user",
  21. name: "user",
  22. component: UserView,
  23. },
  24. ];
  25. const router = createRouter({
  26. history: createWebHistory(process.env.BASE_URL),
  27. routes,
  28. });
  29. export default router;

UserView.vue

  1. <template>
  2. <div class="about">
  3. <h1>This is an User page</h1>
  4. </div>
  5. </template>
相关推荐
阅读 +