This time I will show you how to use Vuejs to operate page regionalization. What are the precautions for Vuejs to operate page regionalization? The following is a practical case, let’s take a look.
Benefits of components
When I use vue to write pages, a large amount of data pages are rendered, and components are introduced to simplify the code amount of the main page. When the code area blocks are almost the same, component encapsulation will simplify the code even more. Components are one of the most powerful features of Vue.js.
Components can extend HTML elements, encapsulating reusable code. At a high level, a component is a custom element to which Vue.js's compiler adds special functionality. In some cases, components can also be in the form of native HTML elements, extended with the is attribute.
I use a book list example from a reading software:
Book display page. You can think about how to use vue to implement the front-end page of this page, and then implement the logical function;
The list display of 'recommended books' and 'latest books' shown in the picture is the same. You can use repeated code to copy the code of 'recommended books' that you have written before and you can easily realize the 'latest books' page.
If other pages also need this display, or I want the code to be more concise, then how to encapsulate the components will come into play
Brief page: Book list display page - Book list component
|- book.vue // 图书展示页面 |-- BookList.vue // 图书列列表组件
For the basic part, I believe everyone who has used vue knows how to use it. I will go directly to the code:
Create a component - Register component - Use component
// 引入组件 import BookList from '../../components/bookList/BookList.vue'; // 注册组件 components:{ BookList, }, // 使用组件 <book-list></book-list>
Vue2.0 stipulates that when introducing components, it is recommended to use camel case naming. Use - to separate them when using them, so that Vue can better identify them
The code that has not been encapsulated before will not be uploaded. Just upload the code:
Book list page - book.vue
|- book.vue - html 页面 <template> <p> </p> <h2 id="欢迎来到波波图书馆">欢迎来到波波图书馆!</h2> <!-- 推荐读书 --> <section> <!-- 大家注意 :books 是BookList.vue组件里图书对象数组 heading 是传给组件的标题 --> <book-list></book-list> </section> <!-- 最新图书 --> <section> <!-- 大家注意 :books 是BookList.vue组件里图书对象数组 heading 是传给组件的标题 --> <book-list></book-list> </section> </template>
I am simulating data. During the development process, I use the API interface to get the data. In fact, they are all the same. There are a lot of codes, but the principles are the same. You can also learn about json by taking a look at it
|- book.vue - js <script> import BookList from '../../components/bookList/BookList.vue'; export default({ data(){ return { // 推荐图书 recommendArray:[ { id:1, img_url: 'https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=671627465,1455045194&fm=173&s=23A2F3039C930EC41A2DB9090300D093&w=640&h=427&img.JPEG', book_name:'Vuejs-1', book_author:'liangfengbo', }, { id:2, img_url: 'https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=671627465,1455045194&fm=173&s=23A2F3039C930EC41A2DB9090300D093&w=640&h=427&img.JPEG', book_name:'Vuejs-2', book_author:'liangfengbo', }, { id:3, img_url: 'https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=671627465,1455045194&fm=173&s=23A2F3039C930EC41A2DB9090300D093&w=640&h=427&img.JPEG', book_name:'Vuejs-3', book_author:'liangfengbo', }, ], // 最新图书 updateBookArray:[ { id:5, img_url: 'https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=671627465,1455045194&fm=173&s=23A2F3039C930EC41A2DB9090300D093&w=640&h=427&img.JPEG', book_name:'Vuejs-5', book_author:'liangfengbo', }, { id:6, img_url: 'https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=671627465,1455045194&fm=173&s=23A2F3039C930EC41A2DB9090300D093&w=640&h=427&img.JPEG', book_name:'Vuejs-6', book_author:'liangfengbo', }, { id:7, img_url: 'https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=671627465,1455045194&fm=173&s=23A2F3039C930EC41A2DB9090300D093&w=640&h=427&img.JPEG', book_name:'Vuejs-7', book_author:'liangfengbo', }, ], } }, // 引入组件 components:{ BookList, }, methods : { }, }) </script>
|- book.vue - css <style> *{ margin: 0; padding: 0; } li{ list-style:none; } .box{ height: auto; border-bottom: 1px solid #efefef; margin: 10px 0; padding: 5px 0; } </style>
Component - BookList.vue
|- 组件 - BookList.vue - html <template> <p> <!-- 头部 --> <!--这个是页面传来的标题 --> </p> <h3 id="heading">{{heading}}</h3> <!-- 列表 --> <article> <!-- 遍历图书数据 --> <li> <router-link>  <!-- 图书图片 --> {{book.book_name}} <!-- 图书名字 --> </router-link> </li> </article> </template>
|- Component - BookList.vue - html
<script> export default({ // props 数据传递的意思 props:[ 'heading',//标题 'books',//图书对象数组 ], data(){ return { } }, methods : { }, }) </script>
|- Component - BookList.vue - css
<style> /*图书列表*/ .book-list { width:100%; height:128px; display: flex; justify-content: space-around; } .heading { border-left: 4px solid #333; margin: 10px 0; padding-left: 4px; } .book-list li { width:80px; height: 100%; flex:1; margin:0 10px; } .book-list li img{ height: 100px; width: 100%; } .book-list li a{ text-align: center; font-size: 12px; text-decoration: none; display: inline-block; width: 100%; } </style>
All the code is here. You can carefully find that component encapsulation is actually the same as our previous JavaScript function encapsulation. It passes parameters, receives parameters, renders data, and reuses it. You can directly copy the code and run it to take a look. There are explanations in the comments. La.
小干物
The parent component calls the child component method:
Write a name on the subcomponent such as:
<start-set-timeout></start-set-timeout>
Calling method: this.$refs.contTimer.countTime(60)
but
Because of data delay, undefined events often occur when calling subcomponents:
TypeError:
Cannot read property 'countTime' of undefined
The solution is
// 调用时加一个定时器 setTimeout(() => { this.$refs.contTimer.countTime(60) }, 20)
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
Detailed explanation of using webpack2 React
JQUERY gets the value of the attribute through the current tag name
The above is the detailed content of How Vuejs operates page regionalization. For more information, please follow other related articles on the PHP Chinese website!

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

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.


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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

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.
