diff --git a/TODO1/understanding-mixins-in-vue-js.md b/TODO1/understanding-mixins-in-vue-js.md index eaa73c44a08..42ea8eb89f9 100644 --- a/TODO1/understanding-mixins-in-vue-js.md +++ b/TODO1/understanding-mixins-in-vue-js.md @@ -1,77 +1,69 @@ -> * 原文地址:[Understanding Mixins in Vue JS](https://blog.bitsrc.io/understanding-mixins-in-vue-js-bdcf9e02a7c1) -> * 原文作者:[Nwose Lotanna](https://medium.com/@viclotana) -> * 译文出自:[掘金翻译计划](https://github.com/xitu/gold-miner) -> * 本文永久链接:[https://github.com/xitu/gold-miner/blob/master/TODO1/understanding-mixins-in-vue-js.md](https://github.com/xitu/gold-miner/blob/master/TODO1/understanding-mixins-in-vue-js.md) -> * 译者: -> * 校对者: +> - 原文地址:[Understanding Mixins in Vue JS](https://blog.bitsrc.io/understanding-mixins-in-vue-js-bdcf9e02a7c1) +> - 原文作者:[Nwose Lotanna](https://medium.com/@viclotana) +> - 译文出自:[掘金翻译计划](https://github.com/xitu/gold-miner) +> - 本文永久链接:[https://github.com/xitu/gold-miner/blob/master/TODO1/understanding-mixins-in-vue-js.md](https://github.com/xitu/gold-miner/blob/master/TODO1/understanding-mixins-in-vue-js.md) +> - 译者:[MacTavish Lee](https://github.com/Reaper622) +> - 校对者:[Baddyo](https://github.com/Baddyo), [jilanlan](https://github.com/jilanlan) -# Understanding Mixins in Vue JS +# 理解 Vue.js 中的 Mixins -A useful introduction to Mixins in Vue, why they are important and how to use them in your workflow. +本文是针对 Vue 中的 Mixins 的实用性介绍,探讨了 Mixins 为何重要,以及如何在工作流中使用。 ![Photo by [Augustine Fou](https://unsplash.com/@augustinefou?utm_source=medium&utm_medium=referral) on [Unsplash](https://unsplash.com?utm_source=medium&utm_medium=referral)](https://cdn-images-1.medium.com/max/12000/0*gtfwEvDVwDgHMv7L) -## What are Mixins? +## 什么是 Mixins? -Mixins in Vue JS are basically a chunk of defined logic, stored in a particular prescribed way by Vue, which can be re-used over and over to add functionality to your Vue instances and components. So Vue mixins can be shared between multiple Vue components without the need to repeat code blocks. Anyone who has used the CSS pre-processor called SASS has a good idea of mixins. +Vue JS 中的 Mixins 基本上是定义一大堆逻辑的地方,它们以 Vue 规定的特殊方式存储,它们可以反复使用来为 Vue 实例和组件添加功能。因此,Vue mixins 可以在多个 Vue 组件之间共享,而不需要重复写代码块。如果你之前用过 Sass 这个 CSS 预处理器,那么你会对 mixins 有很好的理解。 -## Before you start +## 写在开始前 -This post is suited for intermediate frontend developers that use Vue JS, and so being conversant with beginner concepts and installation processes is assumed. Here are a few prerequisites you should already have before you start to use Vue CLI 3 through this article. +这篇文章更适合于使用 Vue JS 开发的中级前端工程师,因此你需要先熟悉基础概念以及安装过程。在开始使用 Vue CLI 3 之前,已经掌握了这些预备知识。 -You will need: +你需要: -* Node.js 10.x and above installed. You can verify if you do by running node -v in your terminal/command prompt. - -* The Node Package Manager 6.7 or above (NPM) also installed. - -* A code editor: Visual Studio Code is highly recommended. ([here’s why](https://blog.bitsrc.io/a-convincing-case-for-visual-studio-code-c5bcc18e1693?source=your_stories_page---------------------------)) - -* Vue latest version installed globally on your machine. - -* Vue CLI 3.0 installed on your machine. To do this, uninstall the old CLI version first: +- 安装了 Node.js 10.x 或者更高的版本。你可以在你的终端运行 node -v 来校验你的版本。 +- 已经安装了 Node Package Manager 6.7 或者更高的版本(NPM)。 +- 一个代码编辑器:高度推荐 Visual Studio Code。 ([推荐理由](https://blog.bitsrc.io/a-convincing-case-for-visual-studio-code-c5bcc18e1693?source=your_stories_page---------------------------)) +- 在你的设备上全局安装了 Vue 的最新版本。 +- 在你的设备上安装了 Vue CLI 3.0。在安装 3.0,先卸载旧版本的 CLI 工具: ``` npm uninstall -g vue-cli ``` -then install the new one: +之后安装新的版本: ``` npm install -g @vue/cli ``` -OR - -* Download a Vue starter project here: +或者 -[**viclotana/vue-canvas**: An optimized and ready-to-use vue application with the default babel and Eslint config](https://github.com/viclotana/vue-canvas) +- 在这里下载一个 Vue 的启动项目: -* Unzip the downloaded project +[**viclotana/vue-canvas**: 一个经过优化、开箱即用的 Vue 应用、配备了默认的 Babel 和 ESlint 的配置](https://github.com/viclotana/vue-canvas) -* Navigate into the unzipped file and run the command to keep all the dependencies up-to-date: +- 解压下载的项目 +- 进入到解压后的文件之后运行下面的命令来保证所有的依赖都是最新的: ``` npm install ``` -## Why are Mixins important - -1. With Vue mixins, you can easily adhere to the DRY principle in programming which simply ensures that you do not repeat yourself. +## 为什么 Mixins 是重要的 -2. With Vue mixins, you also get a great option of flexibility, a mixin object contains options for Vue components so there is a mixing of both mixin and component options. +1. 使用 Vue mixins, 你可以轻松地在编程中遵循 DRY 原则(译者注: Don't Repeat Yourself),它会确保你不会重复自己的代码。 +2. 使用 Vue mixins,你也可以变得十分的灵活,一个 mixins 对象包含有 Vue 组件的选项,所以我们可以将 mixin 与 组件混合使用。 +3. Vue mixins 也是很安全的,如果你的写法恰当,那么它们是不会对超出定义范围的变更产生影响的。 +4. 它们是很棒的代码复用平台。 -3. Vue mixins are also safe, they do not affect changes outside their defined scope if they are well written. +> Mixins 能够灵活地实现为 Vue 组件分发可复用功能。 — 官方文档 -4. They are a great platform for code reusability. +## Mixins 帮助我们解决了什么问题 -> Mixins are a flexible way to distribute reusable functionalities for Vue components — Official Docs +想要充分理解 Vue 中 mixins 的重要性,有一个方式:看看在实际开发中遇到的重用问题。如果你有两个组件,二者中各有一个目的一致或功能相同的方法,就像在下面这个简单的示例中: -## The problem mixins help to solve - -One way to fully understand the importance of mixins in Vue is to see the re-use problem in action. If you have two components that contains a method that does exactly the same thing or performs the exact functionality in the two components like the simple example below: - -Navigate into the project folder and to the src folder and create a components directory where you can then create two components: Test.vue and Modal.vue. Copy the code below to the appropriate components respectively. +进入的你的项目文件夹之后到 src 文件夹创建一个名为 components 的目录,并在其中创建两个组件:Test.vue 和 Modal.vue。复制下面的代码到你的组件中。 ```Vue // Component 1 @@ -93,7 +85,7 @@ export default { }; ``` -The component above displays a button that shows an alert modal when clicked. The second component below does exactly the same thing: +上面的组件展示了一个按钮,点击它会弹出一个警告框。第二个组件会做确切相同的功能: ```Vue // Component 2 @@ -116,7 +108,7 @@ export default { }; ``` -Your App.vue file should have the both components imported and declared exactly like it is below: +你的 App.vue 文件应该引入和声明两个组件,如下所示: ```Vue