Blogger Information
Blog 37
fans 0
comment 1
visits 28105
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
vue基础
kong
Original
169 people have browsed it

vue挂载

  1. <div id="counter">
  2. <p>{{num}}</p>
  3. <p>{{uname}}</p>
  4. </div>
  1. const Counter = {//配置对象
  2. data:function(){
  3. return{
  4. num: 0,
  5. uname: "张三"
  6. }
  7. }
  8. }
  9. let app = Vue.createApp(Counter).mount('#counter') //创建应用,将配置对象传入,mount进行挂载,和上面的元素相关联

文本插值

  1. //最基础的数据绑定形式是文本插值,它使用的是“Mustache”语法(双大括号)
  2. <span>Message{{msg}}</span>

原始HTML

插入html使用v-html指令
永远不要让用户输入HTML内容!

  1. <!-- 输出带标签未解析的html -->
  2. <span>{{ htmlMsg }}</span>
  3. <!-- 输出解析后的html标签 -->
  4. <span v-html="msg"></span>

v-bind 动态绑定属性

语法糖 :

  1. 绑定单个属性
  2. <button :class="className">button</button>
  3. 绑定多个属性
  4. <button v-bin="obj">button</button>
  5. 动态参数 此处 attributeName 与 button 都是变量
  6. <div :[attributeName]="button">容器</div>
  1. export default {
  2. data(){
  3. return {
  4. className:"box",
  5. obj:{
  6. class:"box",
  7. id:"id"
  8. },
  9. attributeName: 'id',
  10. button: 'button'
  11. }
  12. }
  13. }

v-on绑定事件

  1. <a v-on:[eventName]="doSomething"> ... </a>
  2. 语法糖
  3. <a @[eventName]="doSomething">

computed 计算属性

  1. <p>{{ publishedBooksMessage }}</p>
  1. computed:{
  2. //可以理解为,定一个依赖值,只要依赖值不在变动的情况下只计算一次
  3. //计算属性只要依赖值不变,那么不会重新计算,计算属性将基于它们的响应依赖关系缓存
  4. // 一个计算属性的 getter
  5. publishedBooksMessage() {
  6. return this.author.books.length > 0 ? 'Yes' : 'No'
  7. }
  8. },

watch 侦听器

  1. //监听旧值与新值
  2. watch:{ //监听数据变化
  3. msg:function(newValue,OldValue){
  4. conslog.log(OldValue) //输出旧值
  5. conslog.log(newValue) //输出新值
  6. }
  7. }
  8. //监听初始值
  9. watch:{
  10. msg:{
  11. immediate: true, //布尔值开启
  12. handler(newValue){ //固定模板语法
  13. console.log(newValue) //打印初始化的值
  14. }
  15. }
  16. }
  17. //监听对象属性,深度监听
  18. wacth:{
  19. 'obj.name':{
  20. deep: true, //布尔值开启 一层一层遍历 并且都加上一个侦听器
  21. handler(newValue){
  22. console.log(newValue) //进行数据替换/数据更改
  23. }
  24. }
  25. }

Class 类名

  1. <p>class 类名</p>
  2. 第一种放置字符串,正常使用类名
  3. <p class="active">hello</p>
  4. <p>第二种放置对象(常用)</p>
  5. <p :class="{active: true}">hello</p>
  6. <p>第三种和字符串同时存在,不冲突</p>
  7. <p :class="{active: true}" class="hello">hello</p>
  8. <p>使用computed</p>
  9. <p :class="classObjCom">hello</p>
  10. <p>数组语法(不常用)</p>
  11. <p :class="[activeClass,errorClass]">数组语法(不常用)</p>
  12. <p>数组和对象结合</p>
  13. <p :class="[errorClass,{active: true}]">数组和对象结合</p>
  1. export default {
  2. data(){
  3. return {
  4. activeClass: 'active',
  5. errorClass: 'text-danger',
  6. msg:'修饰符',
  7. htmlMsg: '<h1>标题</h1>',
  8. className:"box",
  9. obj:{
  10. id: 'id',
  11. class: 'box'
  12. },
  13. isActive: true,
  14. error: null,
  15. attributeName: 'id',
  16. button: 'button',
  17. author: {
  18. name: 'John Doe',
  19. books: [
  20. 'Vue 2 - Advanced Guide',
  21. 'Vue 3 - Basic Guide',
  22. 'Vue 4 - The Mystery'
  23. ]
  24. }
  25. }
  26. },
  27. computed:{
  28. //计算属性只要依赖值不变,那么不会重新计算,计算属性将基于它们的响应依赖关系缓存
  29. // 一个计算属性的 getter
  30. publishedBooksMessage() {
  31. return this.author.books.length > 0 ? 'Yes' : 'No'
  32. },
  33. classObjCom(){
  34. return{
  35. active: this.isActive && !this.error,
  36. 'text-danger': this.error && this.error.type === 'fatal'
  37. }
  38. }
  39. }
  40. }
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!