Blogger Information
Blog 18
fans 1
comment 0
visits 12207
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Vue的v-bind,v-on, v-model,v-if,v-for,计算属性和侦听器属性 ----0415
木樨
Original
650 people have browsed it

1. 属性指令:v-bind/v-on

  1. <!-- <div class="app" style="color: red" onclick="alert(this.textContent)">hello</div> -->
  2. <div class="app">
  3. <p v-bind:style="style1">{{site}}</p>
  4. <!-- v-bind: 可简单化为":", 为元素绑定普通属性 -->
  5. <p v-bind:style="style1, style2">{{site}}</p>
  6. <p v-bind:style="['background: yellow', 'color: red']">{{site}}</p>
  7. <!-- v-on: 添加事件指令 -->
  8. <!-- <p><a href="https://php.cn" v-on:click="show">网站名称-1</a></p> -->
  9. <!-- v-on: @ -->
  10. <!-- prevent: 修饰符, 防止默认行 -->
  11. <p onclick="console.log(this.tagName)"><a href="https://php.cn" @click.prevent="show">网站名称-1</a></p>
  12. <!-- stop: 防止冒泡 -->
  13. <p onclick="console.log(this.tagName)"><a href="https://php.cn" @click.prevent.stop="show">网站名称-1</a></p>
  14. <!-- 事件传参 -->
  15. <button @click.stop="calc($event, 40, 200)">计算40 + 200 = ?</button>
  16. {{result}}
  17. </div>
  18. <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  19. <script>
  20. const vm = new Vue({
  21. el: ".app",
  22. data: {
  23. site: "php中文网",
  24. style1: "color: red",
  25. style2: "background: yellow",
  26. result: 0,
  27. },
  28. // 事件方法
  29. methods: {
  30. show() {
  31. // this --> vue实例
  32. console.log(this.site);
  33. // debugger;
  34. // ev.preventDefault();
  35. },
  36. calc(ev, x, y) {
  37. this.result = `${x} + ${y} = ${x + y}`;
  38. },
  39. },
  40. });
  41. </script>

2. 双向绑定:v-model

  1. <div class="app">
  2. <p>模型中的数据: {{site}}</p>
  3. <p>
  4. 双向绑定
  5. <input type="text" :value="site" @input="site=$event.target.value" />
  6. </p>
  7. <!-- vue提供一个语法糖来快速实现这个功能: v-model -->
  8. <hr />
  9. <!-- 实时更新绑定 -->
  10. <p>
  11. 双向绑定/实时更新绑定
  12. <input type="text" :value="site" v-model="site" />
  13. </p>
  14. <hr />
  15. <!-- 延迟更新绑定, 修饰符: lazy -->
  16. <p>
  17. 双向绑定/延迟更新绑定
  18. <input type="text" :value="site" v-model.lazy="site" />
  19. </p>
  20. </div>
  21. <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  22. <script>
  23. const vm = new Vue({
  24. el: ".app",
  25. data: {
  26. site: "php中文网",
  27. },
  28. });
  29. // vm.site = "php.cn";
  30. </script>

3. 列表渲染:v-for

  1. <!-- 挂载点 -->
  2. <div class="app">
  3. <!-- 遍历数组 -->
  4. <ul>
  5. <!-- <li>{{items[0]}}</li>
  6. <li>{{items[1]}}</li>
  7. <li>{{items[2]}}</li> -->
  8. <!-- v-for : for of -->
  9. <!-- :key 自定义键属性,主要是功能是干扰diff算法 -->
  10. <li v-for="(item,index) of items" :key="index">[ {{index}} ] => {{item}}</li>
  11. </ul>
  12. <!-- 遍历对象 -->
  13. <ul>
  14. <!-- prop: 字符属性, index: 数值属性 -->
  15. <li v-for="(item,prop, index) of user" :key="index">{{prop}}: {{index}} => {{item}}</li>
  16. </ul>
  17. <!-- 遍历对象数组 -->
  18. <ul>
  19. <li v-for="(user, index) of users" :key="user.id">{{user.id}}: {{user.name}} : [{{user.email}}]</li>
  20. </ul>
  21. </div>
  22. <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  23. <script>
  24. new Vue({
  25. el: ".app",
  26. data: {
  27. // 数组
  28. items: ["合肥", "苏州", "杭州"],
  29. // 对象
  30. user: {
  31. name: "朱老师",
  32. email: "laozhu@qq.com",
  33. },
  34. // 对象数组
  35. users: [
  36. { id: 1, name: "朱老师", email: "laozhu@qq.com" },
  37. { id: 2, name: "西门老师", email: "ximeng@qq.com" },
  38. { id: 3, name: "灭绝老师", email: "miejue@qq.com" },
  39. ],
  40. },
  41. });
  42. // v-for : (循环变量, 索引/键) in/of 数组/对象 :key
  43. // for - of
  44. // for - in
  45. // for , while
  46. </script>

4. 条件渲染:v-if/v-show

  1. <div class="app">
  2. <!-- 单分支 -->
  3. <p v-if="flag">{{msg}}</p>
  4. <button @click="flag = !flag" v-text="tips = flag ? `隐藏`: `显示`"></button>
  5. <!-- 多分支 -->
  6. <p v-if="point > 1000 && point < 2000">{{grade[0]}}</p>
  7. <p v-else-if="point >= 2000 && point < 3000">{{grade[1]}}</p>
  8. <p v-else-if="point >= 3000 && point < 4000">{{grade[2]}}</p>
  9. <p v-else-if="point >= 4000">{{grade[3]}}</p>
  10. <p v-else>{{grade[4]}}</p>
  11. <!-- v-show: 元素依然在源码中,只是display:none -->
  12. <p v-show="state">前端马上就要结束了</p>
  13. </div>
  14. <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  15. <script>
  16. new Vue({
  17. el: ".app",
  18. data() {
  19. return {
  20. msg: "明晚我请大家吃饭,好吗?",
  21. flag: false,
  22. tips: "隐藏",
  23. grade: ["纸片会员", "木头会员", "铁皮会员", "金牌会员", "非会员"],
  24. point: 500,
  25. state: false,
  26. };
  27. },
  28. });
  29. </script>

5. 计算属性

  1. <div class="app">
  2. <p>
  3. 数量:
  4. <input type="number" v-model="num" style="width: 5em" min="0" />
  5. </p>
  6. <p>单价: {{price}} 元</p>
  7. <!-- <p>金额: {{num * price}}</p> -->
  8. <!-- <p>金额: {{res}}</p> -->
  9. <p>金额: {{amount}}</p>
  10. </div>
  11. <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  12. <script>
  13. const vm = new Vue({
  14. el: ".app",
  15. data() {
  16. return {
  17. num: 0,
  18. price: 50,
  19. res: 0,
  20. };
  21. },
  22. // 计算属性
  23. computed: {
  24. amount: {
  25. get() {
  26. return this.num * this.price;
  27. },
  28. set(value) {
  29. console.log(value);
  30. if (value >= 2000) this.price = 40;
  31. },
  32. },
  33. },
  34. methods: {},
  35. });
  36. vm.amount = 2000;
  37. </script>

6. 侦听器属性

  1. <div class="app">
  2. <p>
  3. 数量:
  4. <input type="number" v-model="num" style="width: 5em" min="0" :max="max" />
  5. </p>
  6. <p>单价: {{price}} 元</p>
  7. <p>金额: {{res}}</p>
  8. </div>
  9. <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  10. <script>
  11. const vm = new Vue({
  12. el: ".app",
  13. data() {
  14. return {
  15. num: 0,
  16. price: 50,
  17. res: 0,
  18. max: 100,
  19. };
  20. },
  21. // 侦听器属性
  22. watch: {
  23. // 是用来监听某一个属性的值的变化,属性名要和data字段中的变量名相同
  24. // 例如,我要监听"num"变量的变化
  25. // num(新的值,原来的值)
  26. num(newVal, oldVal) {
  27. console.log(newVal, oldVal);
  28. // this.res = this.num * this.price;
  29. // this.num --> newVal, this.price
  30. this.res = newVal * this.price;
  31. // 监听库存
  32. if (newVal >= 20) {
  33. this.max = newVal;
  34. alert("库存不足, 请进货");
  35. }
  36. },
  37. },
  38. });
  39. </script>
Correcting teacher:天蓬老师天蓬老师

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