Develop using modular approach in vuejs
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 './AppHeader.vue' export default { name:'app', props:['data'] data() { return {} }, methods: { handleClick() {} }, components: { AppHeader } } </script>
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>
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">
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>
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, ''); } 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 + '', valid: false }) } if(jsonObj){ state.jsons.push({obj:jsonObj, valid: true}) } } }
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 '../store' data() { return { state, actions } },
<child :state="state" :handleClick="actions.update"></child>
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;
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:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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 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.

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.

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

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.

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~

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!

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
