前端学习:Vue.js基本使用

简介: 前端学习:Vue.js基本使用

Vue教程文档:

https://cn.vuejs.org/v2/guide/


定义

实例: new Vue()

挂载点: el

数据:data

模板: template

方法:methods

计算属性: computed类似 variable = variable()

侦听器: watch 一旦数据变化都会触发

参数:props

组件:components

组件与实例的关系:每一个组件都是一个Vue实例

父组件与子组件交互:


父组件通过 属性 传递给子组件参数

子组件通过 发布事件$emit 传递给父组件参数,前提是父组件需要先 注册事件

使用

变量使用:使用插值表达式 {{ variable }}

文本替换:v-text="variable"

内容替换:v-html="content"

事件绑定:v-on:click="function" 等价于@click="function"

属性绑定: v-bind:title="variable" 等价于 :title="variable"

双向数据绑定: v-model="variable"

show语句:v-show="bool" 为真时显示

if语句:v-if="bool" 为真时加入dom

for语句:<li v-for="(item, index) of list" :key="index">{{item}}</li>


实例

第一个Vue实例 插值表达式

    <div id="hello">{{ hello }}</div>  
    <script>
        //Vue实例
        new Vue({
            el: "#hello",
            data: {
                hello: "hello world"
            }
        })
    </script>
hello world

挂载点, 实例, 模板

    <div id="root">
    <!-- 挂载点 -->
    </div>
    <script>
        // 实例
        new Vue({
            el: "#root",
            // 模板, 如果实例中没有定义模板,则默认使用挂载点里边的dom元素为模板
            template: "<h1>hello template {{ msg }}</h1>",
            data: {
                msg: "this is message",
            }
        })
    </script>
hello template this is message

文本替换

    <div id="text" v-text="number"></div>
    <script>
        new Vue({
            el: "#text",
            data: {
                number: 123456,
            }
        })
    </script>
123456

内容替换 事件绑定

方法 v-on:等价于@

    <div id="html" v-on:click="handleClick" @dblclick="handleDoubleClick" v-html="content"></div>
    <script>
        new Vue({
            el: "#html",
            data:{
                content: "<h1>this is content</h1>"
            },
            methods: {
                handleClick: function(){
                    this.content = "<h1> click</h1>"
                },
                handleDoubleClick: function(){
                    this.content = "<h1>double click</h1>"
                }
            }
        })
    </script>
this is content

属性绑定

使用v-bind之后,相单于一个js表达式 等价于:title

    <div id="bind" v-bind:title="'hello ' + title">this is title</div>
    <script>
        new Vue({
            el:"#bind",
            data: {
                title: "this is a title"
            }
        })
    </script>
this is title

双向数据绑定

    <div id="db-bind">
        <input type="text" v-model="content">
        <div>{{ content }}</div>
    </div>
    <script>
        new Vue({
            el:"#db-bind",
            data: {
                content: "this is data double bind"
            }
        })
    </script>
this is data doubldasdase bind
this is data doubldasdase bind

计算属性

    <div id="computed">
        <input type="text" v-model="firstName">
        <input type="text" v-model="lastName">
        <div>{{ fullName }}</div>
        <div>{{ count }}</div>
    </div>
    <script>
        new Vue({
            el:"#computed",
            data: {
                firstName: "",
                lastName: "",
                count: 0
            },
            // 计算属性
            computed: {
                fullName: function(){
                    return this.firstName + " " + this.lastName
                }
            },
            // 侦听器, 一旦数据变化都会触发
            watch: {
                fullName: function(){
                    this.count ++
                }
            }
        })
    </script>

v-if v-show v-for

    <div id="vif">
        <div v-if="show">v-if从dom中移除(适合一次操作)</div>
        <div v-show="show">v-show从dom中隐藏(适合多次操作)</div>
    <button @click="handleClick">toggle</button>
    <ul>
        <li v-for="item of list">{{item}}</li>
        <!-- 以下方法增加key,可以提升性能 -->
        <li v-for="(item, index) of list" :key="index">{{item}}</li>
    </ul>
</div>
    <script>
        new Vue({
            el: "#vif",
            data: {
                show: true,
                list: ["first", "second", "third", "fourth"]
            },
            methods: {
                handleClick: function(){
                    this.show = !this.show
                }
            }
        })
    </script>
v-if从dom中移除(适合一次操作)
v-show从dom中隐藏(适合多次操作)
toggle
first
second
third
fourth
first
second
third
fourth

TodoList实例

    <div id="todolist">
        <input type="text" v-model="inputValue">
        <button @click="handleSubmit">确定</button>
        <!-- 普通方式添加 -->
        <ul>
           <global></global>
            <li v-for="(item, index) of list" :key="index">{{ item }}</li>
            <local></local>
        </ul>
        <!-- 通过全局组件 -->
        <ul>
           <todo-item 
                v-for="(item, index) of list" 
                :key="index" 
                :content="item" 
                :index="index"
                @delete="handleDelete"   
                ><!-- 通过全局组件 -->
           </todo-item> 
        </ul>
    </div>
    <script>
        // 全局组件
        Vue.component("global", {
            template: "<li>item-global</li>"
        })
        // 组件与实例的关系:每一个组件都是一个Vue实例,
        Vue.component("todo-item", {
            props: ["content", "index"], //接收参数,通过属性传递值
            template: '<li @click="handleClick">{{content}} {{index}}</li>',  //显示
            methods: {
                handleClick: function(){
                    this.$emit("delete", this.index)  //子组件通过发布和父组件通讯
                }
            }
        })
        // 父组件与子组件通讯,交互
        // 父组件通过 属性 传递给子组件参数
        // 子组件通过 发布事件 传递给父组件参数,前提是父组件需要先 注册事件
        // 局部组件
        var Local = {
            template: "<li>item-local</li>"
        }
        new Vue({
            el:"#todolist",
            // 注册局部组件
            components: {
                "local": Local
            },
            data:{
                inputValue: "",
                list: []
            },
            methods: {
                handleSubmit: function(){
                    this.list.push(this.inputValue);
                    this.inputValue = "";
                },
                handleDelete: function(index){
                    this.list.splice(index, 1)
                }
            }
        })
    </script>
 确定
item-global
sf
fsdfsdf
item-local
sf 0
fsdfsdf 1


相关文章
|
2天前
|
数据采集 人工智能 安全
|
12天前
|
云安全 监控 安全
|
3天前
|
自然语言处理 API
万相 Wan2.6 全新升级发布!人人都能当导演的时代来了
通义万相2.6全新升级,支持文生图、图生视频、文生视频,打造电影级创作体验。智能分镜、角色扮演、音画同步,让创意一键成片,大众也能轻松制作高质量短视频。
1034 151
|
3天前
|
编解码 人工智能 机器人
通义万相2.6,模型使用指南
智能分镜 | 多镜头叙事 | 支持15秒视频生成 | 高品质声音生成 | 多人稳定对话
|
17天前
|
机器学习/深度学习 人工智能 自然语言处理
Z-Image:冲击体验上限的下一代图像生成模型
通义实验室推出全新文生图模型Z-Image,以6B参数实现“快、稳、轻、准”突破。Turbo版本仅需8步亚秒级生成,支持16GB显存设备,中英双语理解与文字渲染尤为出色,真实感和美学表现媲美国际顶尖模型,被誉为“最值得关注的开源生图模型之一”。
1726 9
|
9天前
|
人工智能 自然语言处理 API
一句话生成拓扑图!AI+Draw.io 封神开源组合,工具让你的效率爆炸
一句话生成拓扑图!next-ai-draw-io 结合 AI 与 Draw.io,通过自然语言秒出架构图,支持私有部署、免费大模型接口,彻底解放生产力,绘图效率直接爆炸。
676 152
|
11天前
|
人工智能 安全 前端开发
AgentScope Java v1.0 发布,让 Java 开发者轻松构建企业级 Agentic 应用
AgentScope 重磅发布 Java 版本,拥抱企业开发主流技术栈。
638 13
|
5天前
|
SQL 自然语言处理 调度
Agent Skills 的一次工程实践
**本文采用 Agent Skills 实现整体智能体**,开发框架采用 AgentScope,模型使用 **qwen3-max**。Agent Skills 是 Anthropic 新推出的一种有别于mcp server的一种开发方式,用于为 AI **引入可共享的专业技能**。经验封装到**可发现、可复用的能力单元**中,每个技能以文件夹形式存在,包含特定任务的指导性说明(SKILL.md 文件)、脚本代码和资源等 。大模型可以根据需要动态加载这些技能,从而扩展自身的功能。目前不少国内外的一些框架也开始支持此种的开发方式,详细介绍如下。
397 4