博客列表 >vue常用术语、样式与事件绑定 与 列表渲染学习

vue常用术语、样式与事件绑定 与 列表渲染学习

阿杰
阿杰原创
2022年04月15日 07:36:03501浏览

一、vue常用术语

(1)引入vue3 CDN

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

(2)vue实例

  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>

(3)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)数组的渲染

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