Home Web Front-end JS Tutorial Develop using modular approach in vuejs

Develop using modular approach in vuejs

Jun 20, 2018 pm 03:11 PM
vue.js Modular

This article mainly introduces in detail how to write vuejs in a modular way. The editor thinks it is quite good. Now I will share it with you and give you a reference. Let’s follow the editor to take a look.

Introduction

vuejs is a framework that is simple to get started with, and is easy to use and easy to expand. With the popularity of webpack, vuejs has also launched its own load, vue-loader, which can easily package code. Recently I wrote a json viewer-ac, which fully uses the modular features brought by vue-loader. I was quite happy writing it and gained a lot of experience. Record it here.

File structure

 <template>
  <p>
   <app-header></app-header>
  </p>
</template>
<style>
...
</style>
<script>
  import AppHeader from &#39;./AppHeader.vue&#39;
  export default {
   name:&#39;app&#39;,
   props:[&#39;data&#39;]
   data() {
    return {}
    },
   methods: {
    handleClick() {}
   },
   components: {
    AppHeader
   }
  }
</script>
Copy after login

template contains the template code, which is usually a closed html tag, such as a p.

style contains the css code. This code applies to the entire page. If you only want to apply it to the current template, you need to use the scoped attribute.

 <style scoped>
Copy after login

If you want to use some css preprocessors , such as sass, you only need to declare lang, and then vue-loader will automatically load it. Of course, the premise is that the corresponding sass-loader is installed.

 <style lang="sass">
Copy after login

script Note that the es6 code is used here. I use babel to compile it, so of course I need to install babel, the es6 preset, and build a .babelrc file in the root directory. For details, you can refer to my ac or use the official vue-cli to initialize the project.

template Make invisible tags

template is not only the outermost tag of the template, but it can also be used as an ordinary tag. For example, when we need to use v-if to control the display and hiding of some areas, this can be done.

 <template>
  <template v-if="valid">
    <p></p>
  </template>
  <template v-else>
   <p></p>
  </template>
</template>
Copy after login

Moreover, the template will not be rendered, so it will not affect the final dom structure.

Note: v-show cannot be used with template

flux

During actual development, you will find that the original data management model is confusing. It is difficult to distinguish temporary data, persistent data, user data, and background data. At this time, it is most appropriate to introduce flux.

If you don’t want to introduce other libs for the time being, you can try to implement one yourself. It’s actually very simple. Prepare a store.js

 let trim = str => {
 return str.replace(/(^[\s\t]+)|([\s\t]+$)/g, &#39;&#39;);
}
export const state = {
  jsons: []
}

export const actions = {
  parse(jsonStr) {
    if(!trim(jsonStr)) return

    let jsonObj = null
    try{  
      jsonObj = JSON.parse(jsonStr)
    }catch(err){
      state.jsons.push({err: jsonStr + &#39;&#39;, valid: false })

    }
    if(jsonObj){
      state.jsons.push({obj:jsonObj, valid: true})
    }
  }
}
Copy after login

All view data comes from state. All modifications must be done through actions. Because modifications to data in child components will not affect the parent component, you can safely use the two-way binding feature of vuejs.

Then you can introduce state and actions under the root component of the app, and then pass them to sub-components as needed

import { state, actions } from &#39;../store&#39; data() {
  return {
   state,
   actions
  }
 },
Copy after login
<child :state="state" :handleClick="actions.update"></child>
Copy after login

import public css

Wouldn't it be very convenient and powerful if we store the commonly used style variables on the page in a public file such as common.sass

 $width: 80%;
$height: 100%;
$moli-green:#CCF3E4;
$moli-white:#f8f8f8;
Copy after login

and then introduce them in the component's style.

But unfortunately, I don’t know how to implement this yet.

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

How to implement in Jstree that the disabled child nodes will also be selected when the parent node is selected

In Vue About the usage of filters

Adaptive processing method in Javascript

The above is the detailed content of Develop using modular approach in vuejs. 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)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1262
29
C# Tutorial
1235
24
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.

How to Optimize the Maintainability of Java Code: Experience and Advice How to Optimize the Maintainability of Java Code: Experience and Advice Nov 22, 2023 pm 05:18 PM

How to Optimize the Maintainability of Java Code: Experience and Advice In the software development process, writing code with good maintainability is crucial. Maintainability means that code can be easily understood, modified, and extended without causing unexpected problems or additional effort. For Java developers, how to optimize the maintainability of code is an important issue. This article will share some experiences and suggestions to help Java developers improve the maintainability of their code. Following standardized naming rules can make the code more readable.

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 solve the code complexity error in Python code? How to solve the code complexity error in Python code? Jun 24, 2023 pm 05:43 PM

Python is a simple, easy-to-learn and efficient programming language, but when we write Python code, we may encounter some problems with excessive code complexity. If these problems are not solved, it will make the code difficult to maintain, error-prone, and reduce the readability and scalability of the code. So, in this article, we will discuss how to resolve code complexity error in Python code. Understanding Code Complexity Code complexity is a measure of the nature of code that is difficult to understand and maintain. In Python, there are some indicators that can be used

How to solve the poor maintainability error of Python code? How to solve the poor maintainability error of Python code? Jun 25, 2023 am 11:58 AM

Python, as a high-level programming language, is widely used in software development. Although Python has many advantages, a problem that many Python programmers often face is that the maintainability of the code is poor. The maintainability of Python code includes the legibility, scalability, and reusability of the code. In this article, we will focus on how to solve the problem of poor maintainability of Python code. 1. Code readability Code readability refers to the readability of the code, which is the core of code maintainability.

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~

Teach you step by step how to use Vue3 to implement an elegant element dragging function Teach you step by step how to use Vue3 to implement an elegant element dragging function Mar 30, 2023 pm 08:57 PM

How to implement element dragging function? The following article will take you step by step to understand how to use Vue3 to implement an elegant element dragging function, and learn relevant knowledge points through examples. I hope it will be helpful to you!

2023 JavaScript Framework and Technology Rankings 2023 JavaScript Framework and Technology Rankings Apr 10, 2023 pm 02:11 PM

In short: JavaScript + React + Redux still dominate. Best paired with Next.js and Vercel. AI is advancing rapidly and Web3 is experiencing strong growth. There's been a lot of change over the past year, and it feels like everything is ready to be disrupted, but despite being the most disruptive year I've ever seen, the biggest surprise about this year's framework ecosystem is that very little has changed. While there are a lot of new players entering the market (hurray SolidJS), last year's big winners are still dominating this year and seem to show no signs of giving up in the job market (data to back it up). So what has changed? AI Accelerates Developers When I Made My First Time in 2020

See all articles