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!

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools
