一、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必须保证唯一性

更多相关文章

  1. android通过webservice验证用户
  2. Android(安卓)DataBinding介绍
  3. android Launcher Demo小结
  4. Android(安卓)Service源码分析
  5. 演示classList和表单非空验证
  6. android通过webservice验证用户
  7. android中的Sqlite数据库操作
  8. Android:week 13总结 绑定服务、音乐播放器(服务)
  9. Android(安卓)studio 032 java Tomcat Servlet 搭建服务器

随机推荐

  1. android错误分析集锦
  2. Android实现书籍翻页效果--扩展版(转)
  3. Android android下的数据持久化和读取数
  4. Android(安卓)圆角图片的实现
  5. 【原创】Android 4.4前后版本读取图库图
  6. Android快速开发架构PlanA(一),船新版本的Ba
  7. 系出名门Android(1) - 在 Windows 下搭建
  8. Android公共库——图片缓存 网络缓存 下
  9. strut2服务器与android交互数据
  10. android调用输入软键盘回车键跟删除键