vue.js更改颜色

by Anoob Bava

通过Anoob Bava

如何使用Vue.js实现简单的标题更改应用程序 (How to implement a simple title change application using Vue.js)

Vue.js is a progressive JavaScript framework. It has a lot of features including components, rendering, and routing. The Vue vs React argument is competitive in nature. They both have pros and cons in their field.

Vue.js是一个渐进式JavaScript框架。 它具有很多功能,包括组件,渲染和路由。 Vue vs React的论点本质上是竞争性的。 他们俩在各自领域都各有利弊。

I created a simple JavaScript application in Vue using a CDN (content delivery network). The application has a title which will convert to upper case on clicking a button. I know it is a simple application. But we can learn many simple things through it, like:

我使用CDN(内容交付网络)在Vue中创建了一个简单JavaScript应用程序。 该应用程序的标题将在单击按钮时转换为大写。 我知道这是一个简单的应用程序。 但是我们可以通过它学习很多简单的东西,例如:

  • CDN for VueVue的CDN
  • Vue objectsVue对象
  • linking an attribute to the Vue Object将属性链接到Vue对象
  • defining a data property定义数据属性
  • defining a method using Vue使用Vue定义方法
  • calling the Vue method via listeners通过侦听器调用Vue方法

Okay, let's get our hands dirty!

好吧,让我们动手吧!

I am a big fan of diving the method to chunks, so we will follow the same approach here.

我非常喜欢将该方法应用于大块,因此在这里我们将采用相同的方法。

  1. Create an HTML file and link Vue via CDN.创建一个HTML文件并通过CDN链接Vue。
  2. Create a Vue object.创建一个Vue对象。
  3. Link HTML template to the Vue object.将HTML模板链接到Vue对象。
  4. Create a data property.创建一个数据属性。
  5. Create a method in the Vue object.在Vue对象中创建一个方法。
  6. Change the data when clicking a button.单击按钮时更改数据。

1.创建一个HTML文件并通过CDN链接Vue (1. Create an HTML file and link Vue via CDN)

Initially create a file called index.html. It is our core player. The index.html file contains both the HTML template part and the Vue object.

最初创建一个名为index.html的文件 这是我们的核心参与者。 index.html文件包含HTML模板部分和Vue对象。

I am using Visual Studio Code here.

我在这里使用Visual Studio Code。

Now add the CDN to the index.html. We can use either the development or production version. But it will be good to use the development version for warning and errors. The entry for the development version of the CDN currently is:

现在将CDN添加到index.html。 我们可以使用开发版本或生产版本。 但是将开发版本用于警告和错误会很好。 CDN开发版本的条目当前为:

<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

2.创建一个Vue对象 (2. Create a Vue object)

Now what we need to create is the Vue object inside the index.html file. It is created under the script tag <script></script>.

现在我们需要创建的是index.html文件中的Vue对象。 它是在脚本标记<script> </ script>下创建的。

It can be created by:

可以通过以下方式创建:

new Vue();

The whole syntax is below:

整个语法如下:

<script>new Vue({el: ,
data: {
},
methods: {
}
});</script>

new Vue is an instance of Vue. We can access properties like el, data, methods etc to be with Vue. Properties will be explained below.

new Vuenew Vue的一个实例。 我们可以使用Vue访问诸如el,数据,方法等属性。 属性将在下面说明。

3.将HTML模板链接到Vue对象 (3. Link the HTML template to the Vue object)

As we know Vue has a property called the “el”. This property links the HTML template to the Vue object. In order to do that, all the HTML templates have to be under a single div with an id. For this demo, we can use an id of app. We have added the following to the index.html file:

众所周知,Vue具有称为“ el”的属性。 此属性将HTML模板链接到Vue对象。 为了做到这一点,所有HTML模板都必须在具有ID的单个div下。 对于此演示,我们可以使用app的id。 我们已将以下内容添加到index.html文件:

<div id='app'>
<h3> Welcome to title changer</h3>
</div>

Now, add the app id to the Vue object.

现在,将应用程序ID添加到Vue对象。

new Vue({
el: '#app',
});

So the link will be a success.

因此,链接将成功。

4.创建一个数据属性 (4. Create a data property)

Now we do not want the header “Welcome to title changer” to be static text. We need to be able to display the value from the Vue data property. To do that, Vue has a built-in property called “data.” We need to register that here and use the name in the HTML like below:

现在我们不希望标题“ Welcome to title changer”是静态文本。 我们需要能够显示Vue数据属性中的值。 为此,Vue具有一个称为“数据”的内置属性。 我们需要在这里注册并使用HTML中的名称,如下所示:

new Vue({
el: '#app',
data: {
tile: 'Welcome to title changer'
},
});

Now in the <h3> tag can be updated with the double curly braces like interpolation in Ruby. The value will be:

现在,可以在< h3>标签中使用双花括号(如Ruby中的插值)进行更新。 该值为:

{{title}}

5.在Vue对象中创建方法 (5. Create a method in Vue object)

Vue has a built-in property called the “methods”. This property will support the methods to be declared inside the Vue objects.

Vue具有一个称为“方法”的内置属性。 此属性将支持在Vue对象中声明的方法。

We can use ES6 syntax also. Let me explain them both here.

我们也可以使用ES6语法。 让我在这里解释它们。

methods: {
changeTitle: function() {
this.title = this.title.toUpperCase();
return this.title;
}
}

The ES6 format is:

ES6格式为:

methods: {
changeTitle() {
this.title = this.title.toUpperCase();
return this.title;
}
}

toUpperCase() is simply a JavaScript method which will convert a string to capital letters. An important point to be noted here is, we can access the data property using the this keyword. So the value which is declared in the data property can be accessed using this in the methods.

toUpperCase()只是一个JavaScript方法,它将字符串转换为大写字母。 这里要注意的重要一点是,我们可以使用this关键字访问data属性。 因此,可以在方法中使用this属性访问data属性中声明的值。

6.单击按钮时更改数据 (6. Change the data when clicking a button)

Now, what we do is simply call the method on clicking a button. Simple as that.

现在,我们要做的只是在单击按钮时调用方法。 就那么简单。

To do that, we need to create a button tag.

为此,我们需要创建一个按钮标签。

<button>click to change to upcase</button>

To link the button to the method, we need to use an event handler when clicking the button. Vue provides a built-in listener called v-on.

要将按钮链接到方法,我们需要在单击按钮时使用事件处理程序。 Vue提供了一个称为v-on的内置侦听器

Here is the syntax:

语法如下:

v-on:click="call Action or Method"

We can combine with:

我们可以结合:

<button v-on:click="changeTitle">click to change to upcase</button>

Or we can use a short-hand syntax like below:

或者我们可以使用如下的简写语法:

<button @click="changeTitle">click to change to upcase</button>

That’s it. All done

而已。 全做完了

Before clicking the button, the HTML heading is below:

单击按钮之前,HTML标题如下:

After clicking, it is converted to upper case.

单击后,将转换为大写。

That’s all. Comment if you have any issues or questions. I have attached the Repo details below.

就这样。 如果您有任何问题或疑问,请发表评论。 我已在下面附上了回购详细信息。

github link. I will update with some advanced features in Vue in the coming lessons.

github链接 。 在接下来的课程中,我将更新Vue的一些高级功能。

翻译自: https://www.freecodecamp.org/news/implement-a-simple-title-change-website-using-vue-js-7492e049af7/

vue.js更改颜色

vue.js更改颜色_如何使用Vue.js实现简单的标题更改应用程序相关推荐

  1. vue样式 引入图片_详解Vue.js中引入图片路径的几种方式

    vue --version 3.6.3 记录总结一下的Vue中引入图片路径的几种书写方式 vue中静态资源的引入机制 Vue.js关于静态资源的官方文档 静态资源可以通过两种方式进行处理: 在 Jav ...

  2. vue created 调用方法_深入解析 Vue 的热更新原理,偷学尤大的秘籍?

    大家都用过 Vue-CLI 创建 vue 应用,在开发的时候我们修改了 vue 文件,保存了文件,浏览器上就自动更新出我们写的组件内容,非常的顺滑流畅,大大提高了开发效率.想知道这背后是怎么实现的吗, ...

  3. get vue 和set 用法_深入剖析Vue源码 - 数据代理,关联子父组件

    简单回顾一下这个系列的前两节,前两节花了大篇幅讲了vue在初始化时进行的选项合并.选项配置是vue实例化的第一步,针对不同类型的选项,vue提供的丰富选项配置策略以保证用户可以使用不同丰富的配置选项. ...

  4. vue 二维数组_最近研究Vue源码时我发现的一些好玩函数

    来源 | segmentfault.com/u/chinamasters 作者 | chinamasters 最近在深入研究vue源码,把学习过程中,看到的一些好玩的的函数方法收集起来做分享,希望对大 ...

  5. oclick vue 传参 函数_详解Vue计算属性和侦听属性

    关注[搜狐技术产品]公众号,第一时间获取技术干货 作者介绍: 本期特邀作者:浪里行舟 Github博客2600 star作者,专注于前端领域.个人公众号:「前端工匠」,致力于打造适合初中级工程师能够快 ...

  6. vue 回车查询 按钮_从零开始学习vue

    在github搜小程序我们可以看到许多用许多用vue开发的实例. 到底什么是vue,我们不妨浪费一天时间来了解一下. 一.什么是vue Vue是一个JavaScript框架.作者是国内常年混迹知乎的一 ...

  7. vue插槽面试题_关于前端Vue框架的面试题,面试官可能会问到哪些。?

    这年头,程序员面试都讲究坐姿,姿势不对,努力白费. 参照下图,请同学们对号入座. 回想一下,自己平时面试的坐姿,你在面试官眼里,大概是什么形象,可能是工程师,也可能是键盘侠,或者找麻烦的. 当然了,想 ...

  8. vue 递归创建菜单_如何在Vue中创建类似中等的突出显示菜单

    vue 递归创建菜单 by Taha Shashtari 由Taha Shashtari 如何在Vue中创建类似中等的突出显示菜单 (How to Create a Medium-Like Highl ...

  9. vue 给checkbox 赋值_浅谈vue中关于checkbox数据绑定v-model指令的个人理解

    vue.js为开发者提供了很多便利的指令,其中v-model用于表单的数据绑定很常见, 下面是最常见的例子: {{msg}} js里data初始化数据 new Vue({ el: "#myA ...

最新文章

  1. Oracle表空间文件损坏后的排查及解决
  2. 浏览器检测是否安装flash插件,若没有安装,则弹出安装提示
  3. linux备份目录命令tar,Tar命令备份还原Linux系统
  4. How to Fix “Username is not in the sudoers file. This incident will be reported” in Ubuntu
  5. JavaScript中call和apply方法
  6. 7.QT-Qt对象间的父子关系
  7. Kotlin 知识梳理(9) 委托属性
  8. 夏令时 DST (Daylight Saving Time) java中的夏令时。
  9. TensorFlow精进之路(九):TensorFlow编程基础
  10. latex 插图排版
  11. atlas怎么看日志_[系列文章] Gin框架 - 使用logrus日志记录
  12. 一些javaweb开发常用工具类
  13. python 常用股票走势图绘制
  14. 数组常用操作。以逗号隔开、以逗号+单引号隔开、转List等
  15. ROS文件系统和文件系统操作
  16. Springboot宠物医院系统
  17. 01 | Java入门级学习指南
  18. 2018,程序员生活的两个兴趣爱好
  19. DataFrame.drop_duplicates操作中的inplace参数
  20. 从数据分析的角度看旅行青蛙还可以这么玩。。。

热门文章

  1. Java—格式化日期/时间
  2. 2020.10.s1 冯上
  3. 02 李俊杰 20160221-S1笔试
  4. 格式小结 css 0926
  5. 开通qq邮箱的smtp服务的流程详情
  6. 爬虫-13-认识代理
  7. django-自定义过滤器
  8. 索引-css-第二版-pyhui
  9. ASP程序密码验证漏洞
  10. 【按位dp】文盲的学习方法