博客列表 >vue实例演示课堂上提及的全部指令, 并详细写出常用的术语,以及使用场景

vue实例演示课堂上提及的全部指令, 并详细写出常用的术语,以及使用场景

P粉314265155
P粉314265155原创
2022年08月05日 13:44:10324浏览

>第一个vue页面/静态页面与数据填充

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>第一个vue页面/静态页面与数据填充</title>
  8. <!-- 引入vue -->
  9. <script src="https://unpkg.com/vue@next"></script>
  10. <!-- 引入jquery -->
  11. <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
  12. </head>
  13. <body>
  14. <!-- 静态页面 -->
  15. <h1> 你好</h1>
  16. <!--页面模板化: 模板字面量 -->
  17. <!-- {{message}} 插值 数据占位符 -->
  18. <h2 class="title">{{message}}</h2>
  19. <script>
  20. // es6
  21. // document.querySelector('.title').textContent = 'hello,朱老师';
  22. // jquery
  23. // $('.title').text('小猪');
  24. // vue.js
  25. const app = Vue.createApp({
  26. data () {
  27. return {
  28. message:'小猫',
  29. };
  30. } ,
  31. });
  32. // 方法一
  33. // app.mount(document.querySelector('.title'));
  34. // 方法二
  35. app.mount('.title');
  36. </script>
  37. </body>
  38. </html>

效果

挂载点,vue实例数据注入响应式

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>挂载点,vue实例数据注入响应式</title>
  8. <script src="https://unpkg.com/vue@next"></script>
  9. </head>
  10. <body>
  11. <!-- 1、挂载点:vue实例的作用域 -->
  12. <div class="app">
  13. <p>用户名:{{username}}</p>
  14. </div>
  15. <script>
  16. // // 2.vue实例
  17. // // const app = Vue.createrAPP( 配置项 );
  18. // // Vue.createrAPP() == new Vue
  19. // // function createrAPP (){
  20. // // return new Vue();
  21. // // }
  22. // // 2.1vue配置项
  23. // const config = {
  24. // // vue 中的遍历全部写到 data()方法中
  25. // // data : function(){
  26. // // }
  27. // data () {
  28. // return {
  29. // // 需要返回的的数据
  30. // username:'小猪',
  31. // };
  32. // },
  33. // };
  34. // // 2.2创建VUE的实例
  35. // const app = Vue.createApp(config);
  36. // // 2.3 将实例挂载到页面,挂载点上绑定
  37. // // app.mount(document.querySelector('.app'));
  38. // // 2.3 简写
  39. // app.mount('.app');
  40. // 代码简化
  41. const app1 = Vue.createApp({
  42. data() {
  43. return {
  44. username:'小狗',
  45. };
  46. },
  47. }
  48. ).mount('.app');
  49. console.log('========');
  50. // 3、数据注入 数据自动注入到vue实例中
  51. console.log(app1.$data.username);
  52. // 简化 原因:数据已经被绑定到 vue实例上app1 对象上 app1.username = 'xxxx'
  53. console.log(app1.username);
  54. // 用学过的 访问器属性 模拟数据注入
  55. const obj ={
  56. $data: {
  57. email : '123@qq.com',
  58. },
  59. };
  60. console.log(obj.$data.email);
  61. console.log(obj.email);
  62. console.log('========');
  63. const obj1 ={
  64. $data: {
  65. email : '123@qq.com',
  66. },
  67. get email(){
  68. return this.$data.email;
  69. },
  70. };
  71. // 数据已经注入到obj中
  72. console.log(obj1.email);
  73. // 4、响应式、
  74. app1.username = '小龙';
  75. </script>
  76. </body>
  77. </html>

效果

v-text v-html v-onece(只渲染一次)

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>v-text v-html v-onece(只渲染一次)</title>
  8. <script src="https://unpkg.com/vue@next"></script>
  9. </head>
  10. <body>
  11. <!-- vue 指令 :"v- ''前缀:本质上就是html 标签的自定义属性 -->
  12. <div class="app">
  13. <!-- v-text -> textContent -->
  14. <p>用户名:<span >{{username}}</span></p>
  15. <p>用户名:<span v-text = "username"></span></p>
  16. <p>用户名:<span v-html = "username"></span></p>
  17. </div>
  18. <script>
  19. const app = Vue.createApp({
  20. data(){
  21. return {
  22. username:'小狗',
  23. };
  24. },
  25. }).mount('.app');
  26. app.username = '<i style = "color:red">小猫</i>';
  27. </script>
  28. </body>
  29. </html>

效果

样式绑定

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>样式绑定</title>
  8. <script src="https://unpkg.com/vue@next"></script>
  9. <style>
  10. .ctive {
  11. color:rebeccapurple;
  12. }
  13. .bgc {
  14. background-color: aqua;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div class="app">
  20. <p style="color: yellow;">www.baidu.com</p>
  21. <!-- vue 动态属性设置指令 v-bind :属性名-->
  22. <p v-bind:style="style">www</p>
  23. <p v-bind:style="{color:textColor,backgroundColor:bgc}">www</p>
  24. <p v-bind:style="[base,custom]">cn</p>
  25. <!-- 类样式 class classList -->
  26. <p v-bind:class="'ctive'">bai</p>
  27. <p v-bind:class="ctive">du</p>
  28. <p v-bind:class="{ctive:false}">小猫</p>
  29. <p v-bind:class="{ctive:isActive}">小狗</p>
  30. <p v-bind:class="{ctive:isActive2}">小牛</p>
  31. <p v-bind:class="['ctive','bgc' ]">小龙</p>
  32. <p v-bind:class="[mycolor,mybgc]">小猪</p>
  33. <!-- 简化 v-bind 高频指令,可以简化为 : -->
  34. <p :class="[mycolor,mybgc]">小鸡</p>
  35. </div>
  36. </body>
  37. <script>
  38. const app = Vue.createApp({
  39. data() {
  40. return {
  41. // style:'color:blue; background-color: aqua'
  42. // style:'color:blue',
  43. textColor :'red',
  44. // 注意不能用 background-color 不是字面量合法标识符。 要用驼峰式
  45. bgc:'wheat',
  46. // 数组打包
  47. base : {
  48. border: '1px solid',
  49. background:'green',
  50. },
  51. custom : {
  52. color :'white',
  53. padding:'20px',
  54. },
  55. ctive :'ctive',
  56. isActive: true,
  57. isActive2: false,
  58. mycolor:'ctive',
  59. mybgc:'bgc',
  60. };
  61. },
  62. }).mount('.app');
  63. </script>
  64. </html>

效果

数据双向绑定

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>数据双向绑定</title>
  8. <script src="https://unpkg.com/vue@next"></script>
  9. </head>
  10. <body>
  11. <!-- <p>
  12. <div>ES6:</div>
  13. 下一个兄弟节点
  14. <input type="text" value="" oninput="console.log(this.nextElementSibling.textContent=this.value)"><span></span>
  15. </p> -->
  16. <!-- vue -->
  17. <!-- <div class="app">
  18. <p>
  19. vue 事件指令: v-on 可以简写为 @ $ event 必须要加$ 符号 target 事件对象
  20. <input type="text" v-oninput="comment = $event.target.value" v-blind:value="comment" />
  21. <span>{{comment}}</span>
  22. </p>
  23. </div> -->
  24. <div class="app">
  25. <!-- <p> -->
  26. <!-- v-on: vue的事件指令 -->
  27. <!-- v-on: 高频指令, @表示 -->
  28. <!-- <input type="text" @input="comment = $event.target.value" :value="comment" />
  29. <span>{{comment}}</span>
  30. </p> -->
  31. <!-- $event 事件对象, 在vue不能直接用event -->
  32. <!-- 简写 vue为简化双向数据绑定, 创建一个语法糖: v-mode 指令 -->
  33. <p>
  34. <input type="text" v-model="comment" :value="comment" />
  35. <span>{{comment}}</span>
  36. </p>
  37. <!-- 延迟绑定:修饰符 v-model.lazy 跟blur 事件,失去焦点 change 事件类似,具体两者的的共性
  38. -->
  39. <p>
  40. <input type="text" v-model.lazy="comment1" :value="comment1" />
  41. <span>{{comment1}}</span>
  42. </p>
  43. </div>
  44. <script>
  45. const app = Vue.createApp({
  46. data() {
  47. return {
  48. comment: null,
  49. comment1: null,
  50. };
  51. },
  52. }).mount('.app');
  53. </script>
  54. </body>
  55. </html>

效果

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议