当前位置:首页 > VUE

vue简单实现

2026-01-13 08:39:02VUE

Vue 简单实现步骤

安装 Vue.js 最新版本:

npm install vue@latest

创建 Vue 实例:

import { createApp } from 'vue'

const app = createApp({
  data() {
    return {
      message: 'Hello Vue!'
    }
  }
})

挂载到 DOM:

app.mount('#app')

基础模板语法

显示数据:

<div id="app">
  {{ message }}
</div>

绑定属性:

vue简单实现

<div v-bind:id="dynamicId"></div>
<!-- 简写 -->
<div :id="dynamicId"></div>

条件渲染

使用 v-if:

<p v-if="seen">现在你看到我了</p>
<p v-else>现在你看不到我了</p>

列表渲染

使用 v-for:

<ul>
  <li v-for="item in items" :key="item.id">
    {{ item.text }}
  </li>
</ul>

事件处理

使用 v-on:

vue简单实现

<button v-on:click="reverseMessage">反转消息</button>
<!-- 简写 -->
<button @click="reverseMessage">反转消息</button>

表单输入绑定

使用 v-model:

<input v-model="message" placeholder="编辑我">
<p>输入的是:{{ message }}</p>

组件基础

注册组件:

app.component('todo-item', {
  props: ['todo'],
  template: `<li>{{ todo.text }}</li>`
})

使用组件:

<ol>
  <todo-item
    v-for="item in groceryList"
    :todo="item"
    :key="item.id"
  ></todo-item>
</ol>

标签: 简单vue
分享给朋友:

相关文章

vue实现全文检索

vue实现全文检索

Vue 实现全文检索的方法 客户端全文检索(适用于小型数据集) 使用 flexsearch 或 lunr.js 等轻量级库实现前端全文检索: import FlexSearch from 'fle…

vue实现多选联动

vue实现多选联动

vue实现多选联动的方法 使用v-model绑定数组 在Vue中,可以通过v-model绑定一个数组来实现多选联动。当复选框被选中或取消选中时,数组会自动更新。 <template>…

vue实现屏幕共享

vue实现屏幕共享

Vue 实现屏幕共享的方法 使用 WebRTC 和 getDisplayMedia API 通过 navigator.mediaDevices.getDisplayMedia 捕获屏幕内容,结合 We…

vue实现前端注册

vue实现前端注册

Vue 实现前端注册功能 注册表单设计 使用 Vue 的模板语法创建注册表单,包含用户名、邮箱、密码和确认密码字段。表单需绑定 v-model 实现双向数据绑定。 <template>…

vue 页签实现

vue 页签实现

Vue 页签实现方法 在 Vue 中实现页签功能通常涉及动态组件、路由或状态管理。以下是几种常见的实现方式: 使用动态组件 通过 Vue 的 <component> 动态组件结合 v-f…

vue实现横向导航

vue实现横向导航

vue实现横向导航的方法 使用Flex布局实现横向导航 在Vue中可以通过Flex布局快速实现横向导航。创建一个组件,使用display: flex和flex-direction: row属性使导航项…