1. vue常用术语

  1. <!DOCTYPE html>
  2. <html lang="en">
  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. <!-- cdn方式引入vue -->
  9. <script src="https://unpkg.com/vue@next"></script>
  10. </head>
  11. <body>
  12. <!-- 1. 挂载点:相当于作用域 -->
  13. <!-- 下面这个app就是挂载点 -->
  14. <div class="app">
  15. <!-- 用占位符代表变量 -->
  16. <p>用户名:{{userName}}</p>
  17. <p>超能力:{{power}}</p>
  18. </div>
  19. <script>
  20. // 2. vue实例:就是对象
  21. // Vue.createApp方法创建应用实例
  22. // 用mount挂载到挂载点,即选择器app的DOM元素div
  23. const app = Vue.createApp({
  24. // 挂载点中变量数据写在这,用data方法,挂载后就可显示变量
  25. data() {
  26. return {
  27. // 在这写入属性,每个属性对应挂载点中的变量
  28. userName: "超人",
  29. power: "激光眼",
  30. };
  31. },
  32. }).mount(".app");
  33. // 3. 数据注入
  34. // 访问变量方式,常用
  35. console.log(app.userName);
  36. // 映射到data方法访问
  37. console.log(app.$data.userName);
  38. // 数据注入用访问器属性实现
  39. // 创建对象
  40. const obj = {
  41. // 对象
  42. $data: {
  43. userName: "闪电侠",
  44. },
  45. // 访问器属性返回对象中的userName
  46. get userName() {
  47. return this.$data.userName;
  48. },
  49. };
  50. // 正常访问
  51. console.log(obj.$data.userName);
  52. // 用访问器属性访问
  53. console.log(obj.userName);
  54. // 因此看出,数据注入是用访问器属性实现的
  55. // 4. 响应式
  56. // 修改变量
  57. app.userName = "绿灯侠";
  58. </script>
  59. </body>
  60. </html>

2. vue指令

  • vue指令: v-为前缀的一组指令,写到html标签的属性位置上,本质上讲就是”自定义属性”

    2.1 v-text、v-html、v-once

  1. <!DOCTYPE html>
  2. <html lang="en">
  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. </head>
  9. <body>
  10. <script src="https://unpkg.com/vue@next"></script>
  11. <!-- v-text v-html v-once -->
  12. <div class="app">
  13. <p>用户名:{{userName}}</p>
  14. <!-- v-text属性是vue指令,内容必须是js语句,这里userName是变量 -->
  15. <!-- v-text指令不会解析html标签 -->
  16. <p>用户名:<span v-text="userName"></span></p>
  17. <!-- v-html指令会解析html标签 -->
  18. <p>用户名:<span v-html="userName"></span></p>
  19. <!-- v-once指令:渲染1次,后面赋值不会变化 -->
  20. <p>用户名:<span v-once="userName"></span></p>
  21. </div>
  22. <script>
  23. const app = Vue.createApp({
  24. data() {
  25. return {
  26. userName: "超人",
  27. };
  28. },
  29. }).mount(".app");
  30. app.userName = `<span style="color:blue">闪电侠</h3>`;
  31. </script>
  32. </body>
  33. </html>

2.2 样式绑定

  1. <!DOCTYPE html>
  2. <html lang="en">
  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. <style>
  9. .bgCol {
  10. background-color: aqua;
  11. }
  12. .textCol {
  13. color: bisque;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <script src="https://unpkg.com/vue@next"></script>
  19. <div class="app">
  20. <!-- 一、样式绑定 -->
  21. <!-- 指令v-bind,简化写法:冒号 -->
  22. <!-- 1. 绑定行内样式 -->
  23. <!-- 在属性style前加v-bind:指令,后面属性值用变量 -->
  24. <p v-bind:style="style">php中文网</p>
  25. <!-- 多个样式用对象,属性值用变量 -->
  26. <p :style="{color:textColor,backgroundColor:bgColor}">学习php好网站</p>
  27. <!-- 基本样式3个,定制样式3个:使用数组,每个变量为对象 -->
  28. <button :style="[btnBase,btnCustom]">提交</button>
  29. <!-- 2. 绑定class样式 -->
  30. <!-- 用变量方式 -->
  31. <p :class="bgc">绑定class样式</p>
  32. <!-- 用对象方式:控制样式是否生效 -->
  33. <!-- 注意对象样式bgCol名要用字面量'',否则变成变量了。改变变量show值控制样式是否生效 -->
  34. <p :class="{'bgCol':show}">绑定class样式</p>
  35. <!-- 用数组方式: -->
  36. <p :class="[bgc,textC]">绑定class样式</p>
  37. </div>
  38. <script>
  39. const app = Vue.createApp({
  40. data() {
  41. return {
  42. style: "color:red",
  43. textColor: "blue",
  44. bgColor: "yellow",
  45. btnBase: {
  46. width: "6em",
  47. height: "2em",
  48. border: "none",
  49. },
  50. btnCustom: {
  51. color: "white",
  52. backgroundColor: "blue",
  53. cursor: "pointer",
  54. },
  55. bgc: "bgCol",
  56. show: true,
  57. textC: "textCol",
  58. };
  59. },
  60. }).mount(".app");
  61. </script>
  62. </body>
  63. </html>

2.3 双向绑定

  1. <!DOCTYPE html>
  2. <html lang="en">
  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. <!-- es6方式绑定 -->
  12. <p>
  13. <div>es6绑定</div>
  14. <input type="text" oninput="this.nextElementSibling.textContent=this.value"><span></span>
  15. </p>
  16. <!-- vue绑定 -->
  17. <div class="app">
  18. <p>
  19. <div>vue@v-on命令绑定</div>
  20. <!-- 用事件绑定:vue中绑定事件用v-on: -->
  21. <!-- input给个自定义的值:value绑定到com,com是span标签的点位符 -->
  22. <!-- v-on:input事件绑定,给com赋值为当前标签的值 -->
  23. <input type="text" v-on:input="com = $event.target.value" :value="com"><span>{{com}}</span>
  24. </p>
  25. <p>
  26. <div>vue@v-model命令绑定</div>
  27. <input type="text" v-model="com" :value="com"><span>{{com}}</span>
  28. </p>
  29. <p>
  30. <div>vue@v-model.lazy延迟绑定</div>
  31. <!-- 回车时才显示 -->
  32. <input type="text" v-model.lazy="com" :value="com"><span>{{com}}</span>
  33. </p>
  34. </div>
  35. <script>
  36. const app = Vue.createApp({
  37. data(){
  38. return{
  39. com:'',
  40. }
  41. }
  42. }).mount(".app")
  43. </script>
  44. </body>
  45. </html>

更多相关文章

  1. Android(安卓)自定义RadioButton或CheckBox选择样式
  2. Android(安卓)对话框【Dialog】去除白色边框代码
  3. Android ListView 事件监听 || 关于ListView选中时显示的效果。
  4. Android(安卓)App监听软键盘按键的方式与改变软键盘右下角确定键
  5. Android(安卓)五种不同样式Toast
  6. Android(安卓)Seek自定义样式
  7. Android之Button样式
  8. android之Switch开关
  9. JS获取表单元素、dom树遍历增删改、操作元素内容、自定义属性、

随机推荐

  1. Android按键消息传播流程(WindowManagerSe
  2. android 笔记 --- Android中Menu应用
  3. OrmLite - Lightweight Java ORM Support
  4. TextView的跑马灯效果
  5. Android 4.0 用户输入子系统
  6. Activity 模版样式简介
  7. Android中ExpandableListView的使用
  8. android 对话框(Dialog)使用
  9. Android Sensor Shake(WeChat)
  10. 分享七个非常有用的Android开发工具和工