Home Web Front-end JS Tutorial Vuejs single file component (detailed tutorial)

Vuejs single file component (detailed tutorial)

Jun 06, 2018 pm 05:02 PM
vue.js single file component components

This article mainly introduces the detailed explanation of Vuejs single-file component examples. Friends who need it can refer to it

In the previous examples, we defined the components through Vue.component or components attributes. This method is fine in many small and medium-sized projects, but in complex projects, the following shortcomings are very obvious:

String template: lack of highlighting, troublesome to write, especially multi-line HTML Although the template can be written in an html file, it is too intrusive and is not conducive to component decoupling and separation.

No CSS support: means that when HTML and JavaScript are componentized, CSS is obviously omitted

No build step: limited to using HTML and ES5 JavaScript, not preprocessors .

The single-file component with the .vue extension provided by Vuejs provides solutions to all the above problems.

First introduction to single-file components

It is better to use the source code in the tool and create it in the src directory hello.vue file, the content is as follows:

<template>
 <h2>{{ msg }}</h2>
</template>
<script>
export default {
data () {
return {
msg:&#39;Hello Vue.js 单文件组件~&#39;
}
}
}
</script>
<style>
h2 {
color: green;
}
</style>
Copy after login

Then introduce and use it in app.js:

// ES6 引入模块语法
import Vue from &#39;vue&#39;;
import hello from &#39;./hello.vue&#39;;
new Vue({
 el: "#app",
 template: &#39;<hello/>&#39;,
 components: {
  hello
 }
});
Copy after login

The project cannot be run at this time, because the .vue file webpack cannot be used elsewhere. Yes, it requires the corresponding vue-loader to process it, and careful friends will find that ES6 syntax is used in hello.vue. At this time, you need to use the corresponding syntax conversion loader to convert ES6 into ES5 compatible with mainstream browsers. Syntax, here you need to use the officially recommended babel tool. Install the required loader first:

# hello.vue 文件中使用了 css,所以需要 css-loader 来处理,vue-loader 会自动调用
npm install vue-loader css-loader babel-loader babel-core babel-preset-env --save-dev
Copy after login

Some people wonder why they need to install so many tools after just using babel-loader . This is because many tools are independent, and loader is just In order to cooperate with the bridge used by webpack, here babel-core and babel-preset-env are the core of implementing ES6 to ES5.

We will modify the webpack.config.js configuration as follows:

module.exports = {
 // ...
 module: {
  // 这里用来配置处理不同后缀文件所使用的loader
  rules: [
   {
    test: /.vue$/,
    loader: &#39;vue-loader&#39;
   },
   {
    test: /.js$/,
    loader: &#39;babel-loader&#39;
   }
  ]
 }
}
Copy after login

For babel configuration, we also need to create a .babelrc file in the project root directory to configure Babel presets and other related plug-ins. , the content is as follows:

{
 "presets": [ "env" ]
}
Copy after login

However, although everything is configured, the project will still report an error, and the following error will be reported:

ERROR in ./src/hello.vue
Module build failed: Error: Cannot find module &#39;vue-template-compiler&#39;
Copy after login

Some people are not happy, obviously they installed the dependencies according to the official prompts , and configure it correctly, why does it still report an error? Don't be afraid when you encounter an error. Read what the error is first. It is easy to find that it is because Cannot find module 'vue-template-compiler'. This is because vue-loader also processes .vue files. Need to rely on the vue-template-compiler tool for processing.

At first I didn’t know why the official didn’t directly tell users that they need to install this dependency. After reading vue-loader, I realized that the package.json file contains vue-template-compiler and css-loader As peerDependencies, peerDependencies will not be installed automatically (npm@3.0) during installation, and will only give relevant warnings, so this requires us to install it manually, of course in the .vue file If you need to write CSS, you must also use css-loader, which is also in peerDependencies. Related discussions: https://github.com/vuejs/vue-loader/issues/1158

Now that I know the problem, just install it directly:

npm install vue-template-compiler css-loader --save-dev
Copy after login

Run the project again, on the page Our content appears, no error is reported, ok, you’re done~

Using preprocessor

We have learned to write in .vue css, what if you use the sass preprocessor? First install the modules mentioned in the previous article:

npm install sass-loader node-sass --save-dev
Copy after login

Configuration only takes two steps:

Modify the vue-loader configuration in webpack.config.js

module.exports = {
 // ...
 module: {
  // 这里用来配置处理不同后缀文件所使用的loader
  rules: [
   {
    test: /.vue$/,
    loader: &#39;vue-loader&#39;,
    options: {
     loaders: {
      // 这里也可以使用连写方式,但是不利于自定义话参数配置
      // scss: &#39;vue-style-loader!css-loader!sass-loader&#39;
      scss: [
       {
        loader: &#39;vue-style-loader&#39;
       },
       {
        loader: &#39;css-loader&#39;
       },
       {
        loader: &#39;sass-loader&#39;
       }
      ]
     }
    }
   },
   // ...
  ]
 }
}
Copy after login

. To the style tag in the vue file, add the lang="scss" attribute.

After the configuration is completed, you can happily write sass syntax in the .vue file.

Load global settings file

In actual development, when we write sass files, we often extract the global scss variables and put them into a separate file, but this way there is The problem is that every component that needs to be used needs to be manually @import './styles/_var.scss' , which is very unfriendly. The plug-in sass-resources-loader can help us solve this problem very well. Install it first:

npm install sass-resources-loader --save-dev
Copy after login

Then modify the vue-loader related configuration in the webpack.config.js file:

// ...
{
 test: /.vue$/,
 loader: &#39;vue-loader&#39;,
 options: {
  loaders: {
   scss: [
    {
     loader: &#39;vue-style-loader&#39;
    },
    {
     loader: &#39;css-loader&#39;
    },
    {
     loader: &#39;sass-loader&#39;
    },
    // 看这里,看这里,看这里
    {
     loader: &#39;sass-resources-loader&#39;,
     options: {
      // 这里的resources 属性是个数组,可以放多个想全局引用的文件
      resources: [resolve(&#39;./src/styles/_var.scss&#39;)]
     }
    }
   ]
  }
 }
}
// ...
Copy after login

The configuration is complete, let’s test it again.

Create hello1.vue and hello2.vue files respectively in the src directory:

<!-- hello1.vue -->
<template>
 <h1>{{ msg }}</h1>
</template>
<script>
export default {
name:&#39;hello1&#39;,
data () {
return {
msg:&#39;Hello Vue.js 单文件组件~&#39;
}
}
}
</script>
<stylelang="scss">
h1 {
color: $green;
}
</style>

<!-- hello2.vue -->
<template>
 <h1>{{ msg }}</h1>
</template>
<script>
export default {
name:&#39;hello2&#39;,
data () {
return {
msg:&#39;Hello Vue.js 单文件组件~&#39;
}
}
}
</script>
<stylelang="scss">
h1 {
color: $red;
}
</style>
Copy after login

Then create a styles directory and create a new file _var.scss to store global variables in it:

$green: rgb(41, 209, 41);
$red: rgb(177, 28, 28);
Copy after login

Next, reference the two components in app.js:

import Vue from &#39;vue&#39;;
import hello1 from &#39;./hello1.vue&#39;;
import hello2 from &#39;./hello2.vue&#39;;
new Vue({
 el: "#app",
 template: &#39;<p><hello1/><hello2/></p>&#39;,
 components: {
  hello1,
  hello2
 }
});
Copy after login

Just re-run the project.

Scope style

The single file component provides us with a very convenient function, which is to add the scoped attribute to the style tag , the styles within the tag will only apply to elements in the current component.

接着上面的例子,运行后会发现 hello1.vue 中的 h1 颜色并不是想要的 $green 色,而是被 hello2.vue 中的样式覆盖了。于是分别在 hello1.vue 和 hello2.vue 的 style 标签上添加 scoped 属性,如下:

<!-- hello1.vue -->
<stylelang="scss"scoped>
h1 {
color: $green;
}
</style>
<!-- hello2.vue -->
<stylelang="scss"scoped>
h1 {
color: $red;
}
</style>
Copy after login

这样一来我们的两个 h1 标签颜色都显示正常了。

自定义块

在编写某些开源组件时,有时候我们需要同时维护多个组件和组件说明,但是每次修改就要同时修改 .vue 和 .md 文件,相当麻烦。 .vue 文件的 自定义语言块 功能,就允许我们将 markdown 说明同时写进 .vue 文件中,然后通过插件将其说明部分单独提取到相应的 .md 文件中,这样就可以同时维护说明文档和组件功能了。

比如我们将 hello1.vue 文件修改如下:

<docs>
 # 标题
  这是标题内容,[仓库地址](https://github.com/yugasun/You-Dont-Know-Vuejs)
 ## 子标题
  这是子标题内容
</docs>
<template>
 <h1>{{ msg }}</h1>
</template>
<script>
export default {
name:&#39;hello1&#39;,
data () {
return {
msg:&#39;Hello Vue.js 单文件组件~&#39;
}
}
}
</script>
<stylelang="scss"scoped>
h1 {
color: $green;
}
</style>
Copy after login

然后修改 webpack.config.js 配置:

const path = require(&#39;path&#39;);
// 引入相关插件
const ExtractTextPlugin = require(&#39;extract-text-webpack-plugin&#39;);
function resolve(dir){
 return path.resolve(__dirname, dir);
}
module.exports = {
 // 入口文件
 entry: &#39;./src/app.js&#39;,
 // 编译输出文件
 output: {
  path: resolve(&#39;./&#39;),
  filename: &#39;build.js&#39;
 },
 resolve: {
  alias: {
   // 因为我们这里用的是 require 引入方式,所以应该使用vue.common.js/vue.js/vue.min.js
   &#39;vue$&#39;: &#39;vue/dist/vue.common.js&#39;
  }
 },
 devServer: {
  // 这里定义 webpack-dev-server 开启的web服务的根目录
  contentBase: resolve(&#39;./&#39;)
 },
 module: {
  // 这里用来配置处理不同后缀文件所使用的loader
  rules: [
   {
    test: /.vue$/,
    loader: &#39;vue-loader&#39;,
    options: {
     loaders: {
      scss: [
       {
        loader: &#39;vue-style-loader&#39;
       },
       {
        loader: &#39;css-loader&#39;
       },
       {
        loader: &#39;sass-loader&#39;
       },
       {
        loader: &#39;sass-resources-loader&#39;,
        options: {
         resources: [resolve(&#39;./src/styles/_var.scss&#39;)]
        }
       }
      ],
      docs: ExtractTextPlugin.extract(&#39;raw-loader&#39;)
     }
    }
   },
   {
    test: /.js$/,
    loader: &#39;babel-loader&#39;
   }
  ]
 },
 plugins: [
  new ExtractTextPlugin(&#39;docs.md&#39;)
 ]
}
Copy after login

这里用到了 extract-text-webpack-plugin 导出 text 插件,和 raw-loader ,分别都安装下就行。

然后运行构建命令 npm run build ,等运行结束,根目录下会同时生成一个 docs.md 文件,这就是我们想编写的说明文档。

上面是我整理给大家的,希望今后会对大家有帮助。

相关文章:

集成vue到jquery/bootstrap项目方法?

在vue.js中实现分页中单击页码更换页面内容

在vue2.0组件中如何实现传值、通信

The above is the detailed content of Vuejs single file component (detailed tutorial). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to install the Windows 10 old version component DirectPlay How to install the Windows 10 old version component DirectPlay Dec 28, 2023 pm 03:43 PM

Many users always encounter some problems when playing some games on win10, such as screen freezes and blurred screens. At this time, we can solve the problem by turning on the directplay function, and the operation method of the function is also Very simple. How to install directplay, the old component of win10 1. Enter "Control Panel" in the search box and open it 2. Select large icons as the viewing method 3. Find "Programs and Features" 4. Click on the left to enable or turn off win functions 5. Select the old version here Just check the box

Detailed graphic explanation of how to integrate the Ace code editor in a Vue project Detailed graphic explanation of how to integrate the Ace code editor in a Vue project Apr 24, 2023 am 10:52 AM

Ace is an embeddable code editor written in JavaScript. It matches the functionality and performance of native editors like Sublime, Vim, and TextMate. It can be easily embedded into any web page and JavaScript application. Ace is maintained as the main editor for the Cloud9 IDE and is the successor to the Mozilla Skywriter (Bespin) project.

Explore how to write unit tests in Vue3 Explore how to write unit tests in Vue3 Apr 25, 2023 pm 07:41 PM

Vue.js has become a very popular framework in front-end development today. As Vue.js continues to evolve, unit testing is becoming more and more important. Today we’ll explore how to write unit tests in Vue.js 3 and provide some best practices and common problems and solutions.

How to implement calendar component using Vue? How to implement calendar component using Vue? Jun 25, 2023 pm 01:28 PM

Vue is a very popular front-end framework. It provides many tools and functions, such as componentization, data binding, event handling, etc., which can help developers build efficient, flexible and easy-to-maintain Web applications. In this article, I will introduce how to implement a calendar component using Vue. 1. Requirements analysis First, we need to analyze the requirements of this calendar component. A basic calendar should have the following functions: display the calendar page of the current month; support switching to the previous month or next month; support clicking on a certain day,

VUE3 development basics: using extends to inherit components VUE3 development basics: using extends to inherit components Jun 16, 2023 am 08:58 AM

Vue is one of the most popular front-end frameworks currently, and VUE3 is the latest version of the Vue framework. Compared with VUE2, VUE3 has higher performance and a better development experience, and has become the first choice of many developers. In VUE3, using extends to inherit components is a very practical development method. This article will introduce how to use extends to inherit components. What is extends? In Vue, extends is a very practical attribute, which can be used for child components to inherit from their parents.

Detailed example of vue3 realizing the typewriter effect of chatgpt Detailed example of vue3 realizing the typewriter effect of chatgpt Apr 18, 2023 pm 03:40 PM

When I was working on the chatgpt mirror site, I found that some mirror sites did not have typewriter cursor effects, but only text output. Did they not want to do it? I want to do it anyway. So I studied it carefully and realized the effect of typewriter plus cursor. Now I will share my solution and renderings~

Angular components and their display properties: understanding non-block default values Angular components and their display properties: understanding non-block default values Mar 15, 2024 pm 04:51 PM

The default display behavior for components in the Angular framework is not for block-level elements. This design choice promotes encapsulation of component styles and encourages developers to consciously define how each component is displayed. By explicitly setting the CSS property display, the display of Angular components can be fully controlled to achieve the desired layout and responsiveness.

How to open the settings of the old version of win10 components How to open the settings of the old version of win10 components Dec 22, 2023 am 08:45 AM

Win10 old version components need to be turned on by users themselves in the settings, because many components are usually closed by default. First we need to enter the settings. The operation is very simple. Just follow the steps below. Where are the win10 old version components? Open 1. Click Start, then click "Win System" 2. Click to enter the Control Panel 3. Then click the program below 4. Click "Enable or turn off Win functions" 5. Here you can choose what you want to open

See all articles