Blogger Information
Blog 29
fans 0
comment 0
visits 19508
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
vue常用术语、样式与事件绑定 和 列表渲染实例
千里马遇伯乐
Original
642 people have browsed it

一、vue常用术语

  1. <script src="https://unpkg.com/vue@next"></script>

1.引入vue3 CDN

  1. <div class="title">{{message}}</div>
  2. <script>
  3. const app = Vue.createApp({
  4. data(){
  5. return {
  6. message: 'Hello World!'
  7. }
  8. }
  9. }).mount('.title')
  10. </script>

2.v-html、v-text

  1. <div class="app">
  2. <p>用户名:<span v-text="username"></span></p>
  3. <p>用户名:<span v-html="username2"></span></p>
  4. </div>
  5. <script>
  6. const app = Vue.createApp({
  7. data(){
  8. return {
  9. username: 'Hello php.cn',
  10. username2: '<i style="color:red;">Hello php.cn</i>',
  11. }
  12. }
  13. }).mount('.app')
  14. </script>

实例图片

二、样式与事件绑定

1.样式绑定

  1. <style>
  2. .active{
  3. color: red;
  4. }
  5. </style>
  6. <div class="app">
  7. <p>用户名:<span v-text="username"></span></p>
  8. <p>用户名:<span v-html="username"></span></p>
  9. <p :class="active">{{username}}</p>
  10. </div>
  11. <script>
  12. const app = Vue.createApp({
  13. data(){
  14. return {
  15. username: 'Hello php.cn',
  16. active: 'active'
  17. }
  18. }
  19. }).mount('.app')
  20. </script>

实例图片

2.事件绑定
  1. <div class="app">
  2. <p>用户名:<span v-text="username"></span></p>
  3. <p>用户名:<span v-html="username"></span></p>
  4. <p :class="active">{{username}}</p>
  5. <hr>
  6. <p onclick="alert('Hello')">
  7. <a href="https://www.php.cn/" @click="showUrl($event)">显示网址: </a>
  8. <span class="url">{{url}}</span>
  9. </p>
  10. </div>
  11. <script>
  12. const app = Vue.createApp({
  13. data(){
  14. return {
  15. username: 'Hello php.cn',
  16. active: 'active',
  17. url:''
  18. }
  19. },
  20. methods:{
  21. showUrl(ev){
  22. // 禁用默认行为
  23. ev.preventDefault();
  24. // 防止冒泡
  25. ev.stopPropagation();
  26. // this: 当前vue实例
  27. this.url = ev.target.href;
  28. }
  29. }
  30. }).mount('.app')
  31. </script>

三、列表渲染

数组的渲染
  1. <div class="app">
  2. <p>用户名:<span v-text="username"></span></p>
  3. <p>用户名:<span v-html="username"></span></p>
  4. <p :class="active">{{username}}</p>
  5. <hr>
  6. <p onclick="alert('Hello')">
  7. <a href="https://www.php.cn/" @click="showUrl($event)">显示网址: </a>
  8. <span class="url">{{url}}</span>
  9. </p>
  10. <hr>
  11. <li v-for="(city,index) of cities" :key="index">{{index}}=>{{city}}</li>
  12. </div>
  13. <script>
  14. const app = Vue.createApp({
  15. data(){
  16. return {
  17. username: 'Hello php.cn',
  18. active: 'active',
  19. url:'',
  20. cities:['北京','上海','天津']
  21. }
  22. },
  23. methods:{
  24. showUrl(ev){
  25. // 禁用默认行为
  26. ev.preventDefault();
  27. // 防止冒泡
  28. ev.stopPropagation();
  29. // this: 当前vue实例
  30. this.url = ev.target.href;
  31. }
  32. }
  33. }).mount('.app')
  34. </script>

2.对象渲染

  1. <div class="app">
  2. <p>用户名:<span v-text="username"></span></p>
  3. <p>用户名:<span v-html="username"></span></p>
  4. <p :class="active">{{username}}</p>
  5. <hr>
  6. <p onclick="alert('Hello')">
  7. <a href="https://www.php.cn/" @click="showUrl($event)">显示网址: </a>
  8. <span class="url">{{url}}</span>
  9. </p>
  10. <hr>
  11. <li v-for="(city,index) of cities" :key="index">{{index}}=>{{city}}</li>
  12. <hr>
  13. <li v-for="(item,index) of user" :key="index">{{index}}=>{{item}}</li>
  14. </div>
  15. <script>
  16. const app = Vue.createApp({
  17. data(){
  18. return {
  19. username: 'Hello php.cn',
  20. active: 'active',
  21. url:'',
  22. cities:['北京','上海','天津'],
  23. user:{
  24. name:'马先生',
  25. sex:'男',
  26. address:'宁夏石嘴山市'
  27. }
  28. }
  29. },
  30. methods:{
  31. showUrl(ev){
  32. // 禁用默认行为
  33. ev.preventDefault();
  34. // 防止冒泡
  35. ev.stopPropagation();
  36. // this: 当前vue实例
  37. this.url = ev.target.href;
  38. }
  39. }
  40. }).mount('.app')
  41. </script>
图片实例


:key: 必须要添加,diff算法,key必须保证唯一性

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