首页 > 其他分享 >Vue.js 实例

Vue.js 实例

时间:2024-07-13 08:58:20浏览次数:13  
标签:Vue 实例 js html static https images com jyshare

导航菜单实例

导航菜单

创建一个简单的导航菜单:

<div id="main">
 
    <!-- 激活的菜单样式为  active 类 -->
    <!-- 为了阻止链接在点击时跳转,我们使用了 "prevent" 修饰符 (preventDefault 的简称)。 -->
 
    <nav v-bind:class="active" v-on:click.prevent>
 
        <!-- 当菜单上的链接被点击时,我们调用了 makeActive 方法, 该方法在 Vue 实例中创建。 -->
 
        <a href="#" class="home" v-on:click="makeActive('home')">Home</a>
        <a href="#" class="projects" v-on:click="makeActive('projects')">Projects</a>
        <a href="#" class="services" v-on:click="makeActive('services')">Services</a>
        <a href="#" class="contact" v-on:click="makeActive('contact')">Contact</a>
    </nav>
 
     <!-- 以下 "active" 变量会根据当前选中的值来自动变换 -->
 
    <p>您选择了 <b>{{active}} 菜单</b></p>
</div>
 
<script>
// 创建一个新的 Vue 实例
var demo = new Vue({
    // DOM 元素,挂载视图模型
    el: '#main',
 
    // 定义属性,并设置初始值
    data: {
        active: 'home'
    },
 
    // 点击菜单使用的函数
    methods: {
        makeActive: function(item){
            // 模型改变,视图会自动更新
            this.active = item;
        }
    }
});
</script>

编辑文本实例

文本编辑

点击指定文本编辑内容:

<!-- v-cloak 隐藏未编译的变量,直到 Vue 实例准备就绪。 -->
<!-- 元素点击后 hideTooltp() 方法被调用 -->
 
<div id="main" v-cloak v-on:click="hideTooltip">
 
    <!-- 这是一个提示框
         v-on:click.stop 是一个点击事件处理器,stop 修饰符用于阻止事件传递
         v-if 用来判断 show_tooltip 为 true 时才显示 -->
 
    <div class="tooltip" v-on:click.stop v-if="show_tooltip">
 
        <!-- v-model 绑定了文本域的内容
         在文本域内容改变时,对应的变量也会实时改变  -->
 
        <input type="text" v-model="text_content" />
    </div>
 
    <!-- 点击后调用 "toggleTooltip" 方法并阻止事件传递 -->
 
    <!--  "text_content" 变量根据文本域内容的变化而变化 -->
 
    <p v-on:click.stop="toggleTooltip">{{text_content}}</p>
 
</div>
 
<script>
var demo = new Vue({
    el: '#main',
    data: {
        show_tooltip: false,
        text_content: '点我,并编辑内容。'
    },
    methods: {
        hideTooltip: function(){
            // 在模型改变时,视图也会自动更新
            this.show_tooltip = false;
        },
        toggleTooltip: function(){
            this.show_tooltip = !this.show_tooltip;
        }
    }
})
</script>

订单列表实例

订单列表

创建一个订单列表,并计算总价:

<form id="main" v-cloak>
 
    <h1>Services</h1>
 
    <ul>
 
        <!-- 循环输出 services 数组, 设置选项点击后的样式 -->
 
        <li v-for="service in services" v-on:click="toggleActive(service)" v-bind:class="{ 'active': service.active}">
 
            <!-- 显示订单中的服务名,价格
                 Vue.js 定义了货币过滤器,用于格式化价格 -->
 
            {{service.name}} <span>{{service.price | currency}}</span>
 
        </li>
    </ul>
 
    <div class="total">
 
        <!-- 计算所有服务的价格,并格式化货币 -->
 
        Total: <span>{{total() | currency}}</span>
        
    </div>
 
</form>
<script>
 
// 自定义过滤器 "currency" 
Vue.filter('currency', function (value) {
    return '$' + value.toFixed(2);
});
 
var demo = new Vue({
    el: '#main',
    data: {
        // 定义模型属性
        // 视图将循环输出数组的数据
        services: [
            {
                name: 'Web Development',
                price: 300,
                active:true
            },{
                name: 'Design',
                price: 400,
                active:false
            },{
                name: 'Integration',
                price: 250,
                active:false
            },{
                name: 'Training',
                price: 220,
                active:false
            }
        ]
    },
    methods: {
        toggleActive: function(s){
            s.active = !s.active;
        },
        total: function(){
 
            var total = 0;
 
            this.services.forEach(function(s){
                if (s.active){
                    total+= s.price;
                }
            });
 
           return total;
        }
    }
});
    
</script>

搜索页面实例

搜索页面

在输入框输入搜索内容,列表显示配置的列表:

<form id="main" v-cloak>
 
    <div class="bar">
        <!-- searchString 模型与文本域创建绑定 -->
 
        <input type="text" v-model="searchString" placeholder="输入搜索内容" />
    </div>
 
    <ul>
        <!-- 循环输出数据 -->
             
        <li v-for="article in filteredArticles">
            <a v-bind:href="article.url"><img v-bind:src="article.image" /></a>
            <p>{{article.title}}</p>
        </li>
    </ul>
 
</form>
<script>
 
var demo = new Vue({
    el: '#main',
    data: {
        searchString: "",
 
        // 数据模型,实际环境你可以根据 Ajax 来获取
 
        articles: [
            {
                "title": "What You Need To Know About CSS Variables",
                "url": "https://www.runoob.com/css/css-tutorial.html",
                "image": "https://static.jyshare.com/images/icon/css.png"
            },
            {
                "title": "Freebie: 4 Great Looking Pricing Tables",
                "url": "https://www.runoob.com/html/html-tutorial.html",
                "image": "https://static.jyshare.com/images/icon/html.png"
            },
            {
                "title": "20 Interesting JavaScript and CSS Libraries for February 2016",
                "url": "https://www.runoob.com/css3/css3-tutorial.html",
                "image": "https://static.jyshare.com/images/icon/css3.png"
            },
            {
                "title": "Quick Tip: The Easiest Way To Make Responsive Headers",
                "url": "https://www.runoob.com/css3/css3-tutorial.html",
                "image": "https://static.jyshare.com/images/icon/css3.png"
            },
            {
                "title": "Learn SQL In 20 Minutes",
                "url": "https://www.runoob.com/sql/sql-tutorial.html",
                "image": "https://static.jyshare.com/images/icon/sql.png"
            },
            {
                "title": "Creating Your First Desktop App With HTML, JS and Electron",
                "url": "https://www.runoob.com/js/js-tutorial.html",
                "image": "https://static.jyshare.com/images/icon/html.png"
            }
        ]
    },
    computed: {
        // 计算属性,匹配搜索
        filteredArticles: function () {
            var articles_array = this.articles,
                searchString = this.searchString;
 
            if(!searchString){
                return articles_array;
            }
 
            searchString = searchString.trim().toLowerCase();
 
            articles_array = articles_array.filter(function(item){
                if(item.title.toLowerCase().indexOf(searchString) !== -1){
                    return item;
                }
            })
 
            // 返回过滤后的数据
            return articles_array;;
        }
    }
});
</script>

切换不同布局实例

切换不同布局

点击右上角的按钮来切换不同页面布局:

<form id="main" v-cloak>
 
    <div class="bar">
 
        <!-- 两个按钮用于切换不同的列表布局 -->
 
        <a class="list-icon" v-bind:class="{ 'active': layout == 'list'}" v-on:click="layout = 'list'"></a>
        <a class="grid-icon" v-bind:class="{ 'active': layout == 'grid'}" v-on:click="layout = 'grid'"></a>
    </div>
 
    <!-- 我们设置了两套布局页面。使用哪套依赖于 "layout" 绑定 -->
 
    <ul v-if="layout == 'grid'" class="grid">
        <!-- 使用大图,没有文本 -->
        <li v-for="a in articles">
            <a v-bind:href="a.url" target="_blank" rel="noopener noreferrer"><img v-bind:src="a.image.large" /></a>
        </li>
    </ul>
 
    <ul v-if="layout == 'list'" class="list">
        <!-- 使用小图及标题 -->
        <li v-for="a in articles">
            <a v-bind:href="a.url" target="_blank" rel="noopener noreferrer"><img v-bind:src="a.image.small" /></a>
            <p>{{a.title}}</p>
        </li>
    </ul>
 
</form>
<script>
 
    var demo = new Vue({
        el: '#main',
        data: {
            // 视图模型,可能的值是 "grid" 或 "list"。
            layout: 'grid',
 
            articles: [{
                "title": "HTML 教程",
                "url": "https://www.runoob.com/html/html-tutorial.html",
                "image": {
                    "large": "https://static.jyshare.com/images/mix/htmlbig.png",
                    "small": "https://static.jyshare.com/images/icon/html.png"
                }
            },
            {
                "title": "CSS 教程",
                "url": "https://www.runoob.com/css/css-tutorial.html",
                "image": {
                    "large": "https://static.jyshare.com/images/mix/cssbig.png",
                    "small": "https://static.jyshare.com/images/icon/css.png"
                }
            },
            {
                "title": "JS 教程",
                "url": "https://www.runoob.com/js/js-tutorial.html",
                "image": {
                    "large": "https://static.jyshare.com/images/mix/jsbig.jpeg",
                    "small": "https://static.jyshare.com/images/icon/js.png"
                }
            },
            {
                "title": "SQL  教程",
                "url": "https://www.runoob.com/sql/sql-tutorial.html",
                "image": {
                    "large": "https://static.jyshare.com/images/mix/sqlbig.png",
                    "small": "https://static.jyshare.com/images/icon/sql.png"
                }
            },
            {
                "title": "Ajax 教程",
                "url": "https://www.runoob.com/ajax/ajax-tutorial.html",
                "image": {
                    "large": "https://static.jyshare.com/images/mix/ajaxbig.png",
                    "small": "https://static.jyshare.com/images/icon/ajax.png"
                }
            },
            {
                "title": "Python 教程",
                "url": "https://www.runoob.com/pyhton/pyhton-tutorial.html",
                "image": {
                    "large": "https://static.jyshare.com/images/mix/pythonbig.png",
                    "small": "https://static.jyshare.com/images/icon/python.png"
                }
            }]
 
        }
    });
    
</script>

 

 

标签:Vue,实例,js,html,static,https,images,com,jyshare
From: https://blog.csdn.net/lwx666sl/article/details/140223727

相关文章

  • Vue打包文件dist放在SpringBoot项目下运行(正确实现全过程)
    项目开发中,一般我们都会使用SpringBoot+Vue进行前后端开发。在部署时,会后端启动一个服务,再启动一个nginx,nginx中配置前端打包文件dist进行项目访问。实际上,我们也可以把打包好的dist目录放在SpringBoot项目下进行部署。将dist包放入resources下配置拦截器@Configuration......
  • Vue路由传参和接参如何实现
    在Vue中,使用VueRouter进行页面路由跳转时,经常需要传递参数到目标页面(组件)并在目标页面(组件)中接收这些参数。VueRouter提供了几种方式来实现路由传参和接参,主要包括通过URL的查询参数(query)、动态路由匹配(params)以及命名路由配合params或query使用。下面将分别介绍这几种方式。......
  • 在Vue中,子组件向父组件传递数据
    在Vue中,子组件向父组件传递数据通常通过两种方式实现:事件和回调函数。这两种方式允许子组件与其父组件进行通信,传递数据或触发特定的行为。1.通过事件传递数据子组件可以通过触发自定义事件,并将数据作为事件的参数来向父组件传递数据。子组件:<template><div>......
  • idea启动vue项目一直卡死在51%,问题分析及其如何解决
    如果你的项目也一直卡在百分之几十,你可以参考下面的方法,试一试能否解决问题描述:通过在idea终端中输入命令npmrunserve启动vue项目,启动进程一直卡在51%如何解决:检查<template>标签中的html内容是否被唯一一个根标签包裹......
  • zdppy+onlyoffice+vue3解决文档加载和文档强制保存时弹出警告的问题
    解决过程第一次排查最开始排查的是官方文档说的https://api.onlyoffice.com/editors/troubleshooting#key解决方案。参考的是官方的https://github.com/ONLYOFFICE/document-server-integration/releases/latest/download/Python.Example.zip基于Django的Python代码。......
  • Vue2与Vue3的区别
    Vue2官方已经发布不再维护了,很多公司也开始纷纷使用vue3了。那么vue3和vue2有哪些不同呢?vue3(下面简称v3)又在vue2(下面简称v2)上做了哪些拓展与改进呢?(1)首先最大的区别是数据双向绑定的改进,v2用的是es5的数据劫持Object.definedProperty,这个会一开始就给所有的数据设置了监听,比......
  • 主流json解析框架示例
    主流json解析框架示例jackson、gson、fastjson/fastjson2三种主流json解析框架对比●性能:在性能方面,Fastjson通常被认为是最快的JSON解析库,其次是Jackson和Gson,json-lib的性能相对较低。●API和功能:Jackson提供了非常灵活、强大的API,支持各种高级功能,例如树模型、数据绑定、......
  • 简易Token认证系统实现指南(Spring Boot - Vue)
    在现代Web应用中,用户认证是一个不可或缺的部分。除了传统的会话/cookie认证方式,Token认证提供了一种无状态、可扩展的认证机制。在本文中,我将向您展示如何在一个SpringBoot应用中实现一个简易的Token认证系统 什么是Token认证?Token认证是一种安全机制,通常使用JSONWebToke......
  • ExtJs开发教程_001_Ext.data.Store使用方法详解
    本系列教程基本可以看做是ExtJSAPI中文版+实例演示更多内容请参看:http://www.cnblogs.com/mryeExt.data.Store用法介绍这个组件继承自Ext.data.AbstractStore 本篇讲解了如何构造并且做一些基本使用,如果有什么疑问可以联系我QQ1330771552下面是他的属性列表?aut......
  • ExtJs常用布局--layout详解(含实例)
    序言:笔者用的ExtJs版本:ext-3.2.0ExtJs常见的布局方式有:border、form、absolute、column、accordion、table、fit、card、anchor另外,不常见的布局有:tab、vbox、hbox本文所有实例代码已提供下载,下载链接:ExtJs常用布局--layout详解实例代码 简介:最常用的边框布局——BorderLa......