博主信息
博文 33
粉丝 0
评论 0
访问量 24960
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
vue 常用术语,样式与事件绑定,列表渲染的实例演示
lucaslwk
原创
521人浏览过

vue 常用术语,样式与事件绑定,列表渲染的实例演示

一.vue 常用术语

vue常用术语

  1. <!-- 挂载点,当前vue实例的作用域 -->
  2. <div class="userName">
  3. <p>用户名:{{username}}</p>
  4. </div>
  5. <script>
  6. // vue实例
  7. const app = Vue.createApp({
  8. data() {
  9. return {
  10. username: "张三",
  11. };
  12. },
  13. }).mount(".userName");
  14. //数据注入
  15. console.log(app.username);
  16. //完整注入
  17. console.log(app.$data.username);
  18. //响应式:实时响应外部更新
  19. app.username = "李四";
  20. </script>

二.vue 样式与事件绑定

vue样式与事件绑定

  1. <!-- 样式 -->
  2. <div class="userName">
  3. <p>用户名:<span v-text="username"></span></p>
  4. <p>邮箱:<span v-html="email"></span></p>
  5. <!-- <p v-bind:style="{color,border}">行内样式</p> -->
  6. <p :style="{color,border}">行内样式</p>
  7. <!-- <p v-bind:class="active">类样式</p> -->
  8. <p :class="active">类样式</p>
  9. </div>
  10. <!-- 事件绑定 -->
  11. <div class="input">
  12. <!-- v:on事件属性,v-on可简化为@,event:$event -->
  13. <input type="text" v-on:input="content=$event.target.value" />
  14. <span>{{content}}</span>
  15. <!--:value回显 -->
  16. <input type="text" @input="content=$event.target.value" :value="content" />
  17. <span>{{content}}</span>
  18. <!-- v-model -->
  19. <input type="text" v-model="content" />
  20. <span>{{content}}</span>
  21. <!-- v-model.lazy延迟响应,失去焦点时响应 -->
  22. <input type="text" v-model.lazy="content" />
  23. <span>{{content}}</span>
  24. </div>
  25. <div class="net" onclick="console.log('hello')">
  26. <a href="http://php.cn" @click="showUrl($event)">网站链接:</a>
  27. <span>{{url1}}</span>
  28. <!-- .prevent替换默认行为,.stop防止冒泡 -->
  29. <a
  30. href="http://www.baidu.com"
  31. @click.prevent.stop="this.url2=$event.target.href"
  32. >网站链接:</a
  33. >
  34. <span>{{url2}}</span>
  35. </div>
  36. <script>
  37. const app = Vue.createApp({
  38. data() {
  39. return {
  40. username: "李四",
  41. email: "<a style='color:red'>123456@qq.com</a>",
  42. color: "blue",
  43. border: "1px solid black",
  44. active: "active",
  45. };
  46. },
  47. }).mount(".userName");
  48. const app2 = Vue.createApp({
  49. data() {
  50. return { content: "" };
  51. },
  52. }).mount(".input");
  53. const app3 = Vue.createApp({
  54. data() {
  55. return { url1: "", url2: "" };
  56. },
  57. methods: {
  58. showUrl(event) {
  59. event.preventDefault();
  60. event.stopPropagation();
  61. this.url1 = event.target.href;
  62. },
  63. },
  64. }).mount(".net");
  65. </script>

三.vue 列表渲染

vue列表渲染

  1. <div class="app">
  2. <!-- :key必须要添加,diff算法,key必须保证唯一性 -->
  3. <!-- 数组 -->
  4. <li v-for="(name,index) of names" :key="index">{{name}}</li>
  5. <!-- 对象 -->
  6. <li v-for="(item,key) of classes" :key="key">{{item}}</li>
  7. </div>
  8. <script>
  9. const app = Vue.createApp({
  10. data() {
  11. return {
  12. names: ["张三", "李四", "王五"],
  13. classes: {
  14. id: "一班",
  15. school: "第一小学",
  16. },
  17. };
  18. },
  19. }).mount(".app");
  20. </script>
批改老师:PHPzPHPz

批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学