ASCII码 ASCII码

vue 常用术语,样式与事件绑定,列表渲染的实例演示

发布于:2022-04-17 13:28:16  栏目:技术文档

vue 常用术语,样式与事件绑定,列表渲染的实例演示

一.vue 常用术语

vue常用术语

  1. <!-- 挂载点,当前vue实例的作用域 -->
  2. <div class="userName">
  3. <p>用户名:{{username}}</p>
  4. </div>
  5. <script>
  6. // vue实例
  7. const app = Vue.createApp({
  8. data() {
  9. return {
  10. username: "张三",
  11. };
  12. },
  13. }).mount(".userName");
  14. //数据注入
  15. console.log(app.username);
  16. //完整注入
  17. console.log(app.$data.username);
  18. //响应式:实时响应外部更新
  19. app.username = "李四";
  20. </script>

二.vue 样式与事件绑定

vue样式与事件绑定

  1. <!-- 样式 -->
  2. <div class="userName">
  3. <p>用户名:<span v-text="username"></span></p>
  4. <p>邮箱:<span v-html="email"></span></p>
  5. <!-- <p v-bind:style="{color,border}">行内样式</p> -->
  6. <p :style="{color,border}">行内样式</p>
  7. <!-- <p v-bind:class="active">类样式</p> -->
  8. <p :class="active">类样式</p>
  9. </div>
  10. <!-- 事件绑定 -->
  11. <div class="input">
  12. <!-- v:on事件属性,v-on可简化为@,event:$event -->
  13. <input type="text" v-on:input="content=$event.target.value" />
  14. <span>{{content}}</span>
  15. <!--:value回显 -->
  16. <input type="text" @input="content=$event.target.value" :value="content" />
  17. <span>{{content}}</span>
  18. <!-- v-model -->
  19. <input type="text" v-model="content" />
  20. <span>{{content}}</span>
  21. <!-- v-model.lazy延迟响应,失去焦点时响应 -->
  22. <input type="text" v-model.lazy="content" />
  23. <span>{{content}}</span>
  24. </div>
  25. <div class="net" onclick="console.log('hello')">
  26. <a href="http://php.cn" @click="showUrl($event)">网站链接:</a>
  27. <span>{{url1}}</span>
  28. <!-- .prevent替换默认行为,.stop防止冒泡 -->
  29. <a
  30. href="http://www.baidu.com"
  31. @click.prevent.stop="this.url2=$event.target.href"
  32. >网站链接:</a
  33. >
  34. <span>{{url2}}</span>
  35. </div>
  36. <script>
  37. const app = Vue.createApp({
  38. data() {
  39. return {
  40. username: "李四",
  41. email: "<a style='color:red'>123456@qq.com</a>",
  42. color: "blue",
  43. border: "1px solid black",
  44. active: "active",
  45. };
  46. },
  47. }).mount(".userName");
  48. const app2 = Vue.createApp({
  49. data() {
  50. return { content: "" };
  51. },
  52. }).mount(".input");
  53. const app3 = Vue.createApp({
  54. data() {
  55. return { url1: "", url2: "" };
  56. },
  57. methods: {
  58. showUrl(event) {
  59. event.preventDefault();
  60. event.stopPropagation();
  61. this.url1 = event.target.href;
  62. },
  63. },
  64. }).mount(".net");
  65. </script>

三.vue 列表渲染

vue列表渲染

  1. <div class="app">
  2. <!-- :key必须要添加,diff算法,key必须保证唯一性 -->
  3. <!-- 数组 -->
  4. <li v-for="(name,index) of names" :key="index">{{name}}</li>
  5. <!-- 对象 -->
  6. <li v-for="(item,key) of classes" :key="key">{{item}}</li>
  7. </div>
  8. <script>
  9. const app = Vue.createApp({
  10. data() {
  11. return {
  12. names: ["张三", "李四", "王五"],
  13. classes: {
  14. id: "一班",
  15. school: "第一小学",
  16. },
  17. };
  18. },
  19. }).mount(".app");
  20. </script>
相关推荐
阅读 +