Vue.js 专为 初学者 解读

Vue 是一套用于构建用户界面的渐进式框架

Vue.js 的核心是一个允许采用简洁的模板语法来声明式地将数据渲染进 DOM 的系统

<div id="app">
  {
    
    {
    
     message }}
</div>
var app = new Vue({
    
    
  el: '#app',
  data: {
    
    
    message: 'Hello Vue!'
  }
})
实现效果

在这里插入图片描述

<div id="app-2">
  <span v-bind:title="message">
    鼠标悬停几秒钟查看此处动态绑定的提示信息!
  </span>
</div>
var app2 = new Vue({
    
    
  el: '#app-2',
  data: {
    
    
    message: '页面加载于 ' + new Date().toLocaleString()
  }
})
实现效果

在这里插入图片描述

小知识
new Date().toLocaleString() 默认显示的格式是 yyyy-mm-dd hh:mm:ss
new Date()的默认格式是 Thu Mar 31 2022 14:30:20 GMT+0800 (中国标准时间)

留给大家一个小问题 大家评论区作答

为什么它这个功能需要鼠标悬浮几秒后才能查看到悬浮的内容呢? 有没有办法将它改成悬浮立刻出现?

<div id="app-5">
  <p>{
   
   { message }}</p>
  <button v-on:click="reverseMessage">反转消息</button>
</div>
var app5 = new Vue({
    
    
  el: '#app-5',
  data: {
    
    
    message: 'Hello Vue.js!'
  },
  methods: {
    
    
    reverseMessage: function () {
    
    
      this.message = this.message.split('').reverse().join('')
    }
  }
})

组件系统是 Vue 的另一个重要概念,因为它是一种抽象,允许我们使用小型、独立和通常可复用的组件构建大型应用。仔细想想,几乎任意类型的应用界面都可以抽象为一个组件树:

// 定义名为 todo-item 的新组件
Vue.component('todo-item', {
    
    
  template: '<li>这是个待办项</li>'
})

var app = new Vue(...)
<ol>
  <!-- 创建一个 todo-item 组件的实例 -->
  <todo-item></todo-item>
</ol>
Vue.component('todo-item', {
    
    
  // todo-item 组件现在接受一个
  // "prop",类似于一个自定义 attribute。
  // 这个 prop 名为 todo。
  props: ['todo'],
  template: '<li>{
    
    { todo.text }}</li>'
})

猜你喜欢

转载自blog.csdn.net/weixin_50001396/article/details/123870773
今日推荐