search
HomeWeb Front-endFront-end Q&AWhat should we pay attention to in vue components?

Things to note for vue components: 1. The template of the component must have and only one root node; 2. The data option of the component must be a function, and the function body returns an object; 3. Subcomponents cannot Directly modify the value passed by the parent component, otherwise there will be a warning.

What should we pay attention to in vue components?

The operating environment of this tutorial: windows7 system, vue2.9.6 version, DELL G3 computer.

Vue componentization considerations

  • Components can be understood as special Vue instances that manage their own templates.

  • The template of the component must have one and only one root node.
  • The data option of the component must be a function, and the function body returns an object.

  • Subcomponent cannot directly modify the value passed by the parent component.

    The parent component can pass values ​​to the child component at will through the attributes, but the child component cannot modify the data of the parent component; otherwise there will be a warning not to directly modify the value passed by the parent component.

    What should we pay attention to in vue components?

Components are independent of each other, and you can configure some of your own option resources data, methods, computed, etc.

Ideology: Components manage themselves and do not affect others.

Component examples

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>全局注册 - 王佳伟</title>
</head>

<body>


    <div id="app">
        <!-- 引用组件时必须使用横线分割符 -->
        <component-a></component-a>
        <component-a></component-a>
        <component-b></component-b>
    </div>

    <script src="./node_modules/vue/dist/vue.js"></script>

    <script>
        // 全局注册组件
        /***
         * 1.组件命名:驼峰、横线分隔符命名方式
         * 2.使用组件:使用组件时必须采用横线分隔符的方式进行引用
         * 
         * 组件可以理解为就是特殊的Vue实例,不需要手动的实例化而已,它用于管理自己的模板。
         */
        Vue.component(&#39;component-a&#39;, {
            // template选项,指定组件的模板代码
            template: &#39;<div><h1 id="头部组件-nbsp-nbsp-nbsp-nbsp-name">头部组件  --  {{name}}</h1></div>&#39;,
            data: function () {  // 在组件中,data选项必须是一个函数
                return {
                    name: &#39;全局组件&#39;
                }
            }
        })

        // 定义局部组件对象
        const ComponentB = {
            template: &#39;<div>这是 {{name}} </div>&#39;,
            data() {
                return {
                    name: &#39;局部组件&#39;
                }
            },
        }
        new Vue({
            el: &#39;#app&#39;,
            components: {
                // key:value   ;  key为组件名,value是组件对象。
                &#39;component-b&#39;: ComponentB
            },
            data: {

            }
        })
    </script>

</body>

</html>

[Related recommendations: "vue.js tutorial"]

The above is the detailed content of What should we pay attention to in vue components?. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
CSS: Can I use multiple IDs in the same DOM?CSS: Can I use multiple IDs in the same DOM?May 14, 2025 am 12:20 AM

No,youshouldn'tusemultipleIDsinthesameDOM.1)IDsmustbeuniqueperHTMLspecification,andusingduplicatescancauseinconsistentbrowserbehavior.2)Useclassesforstylingmultipleelements,attributeselectorsfortargetingbyattributes,anddescendantselectorsforstructure

The Aims of HTML5: Creating a More Powerful and Accessible WebThe Aims of HTML5: Creating a More Powerful and Accessible WebMay 14, 2025 am 12:18 AM

HTML5aimstoenhancewebcapabilities,makingitmoredynamic,interactive,andaccessible.1)Itsupportsmultimediaelementslikeand,eliminatingtheneedforplugins.2)Semanticelementsimproveaccessibilityandcodereadability.3)Featureslikeenablepowerful,responsivewebappl

Significant Goals of HTML5: Enhancing Web Development and User ExperienceSignificant Goals of HTML5: Enhancing Web Development and User ExperienceMay 14, 2025 am 12:18 AM

HTML5aimstoenhancewebdevelopmentanduserexperiencethroughsemanticstructure,multimediaintegration,andperformanceimprovements.1)Semanticelementslike,,,andimprovereadabilityandaccessibility.2)andtagsallowseamlessmultimediaembeddingwithoutplugins.3)Featur

HTML5: Is it secure?HTML5: Is it secure?May 14, 2025 am 12:15 AM

HTML5isnotinherentlyinsecure,butitsfeaturescanleadtosecurityrisksifmisusedorimproperlyimplemented.1)Usethesandboxattributeiniframestocontrolembeddedcontentandpreventvulnerabilitieslikeclickjacking.2)AvoidstoringsensitivedatainWebStorageduetoitsaccess

HTML5 goals in comparison with older HTML versionsHTML5 goals in comparison with older HTML versionsMay 14, 2025 am 12:14 AM

HTML5aimedtoenhancewebdevelopmentbyintroducingsemanticelements,nativemultimediasupport,improvedformelements,andofflinecapabilities,contrastingwiththelimitationsofHTML4andXHTML.1)Itintroducedsemantictagslike,,,improvingstructureandSEO.2)Nativeaudioand

CSS: Is it bad to use ID selector?CSS: Is it bad to use ID selector?May 13, 2025 am 12:14 AM

Using ID selectors is not inherently bad in CSS, but should be used with caution. 1) ID selector is suitable for unique elements or JavaScript hooks. 2) For general styles, class selectors should be used as they are more flexible and maintainable. By balancing the use of ID and class, a more robust and efficient CSS architecture can be implemented.

HTML5: Goals in 2024HTML5: Goals in 2024May 13, 2025 am 12:13 AM

HTML5'sgoalsin2024focusonrefinementandoptimization,notnewfeatures.1)Enhanceperformanceandefficiencythroughoptimizedrendering.2)Improveaccessibilitywithrefinedattributesandelements.3)Addresssecurityconcerns,particularlyXSS,withwiderCSPadoption.4)Ensur

What are the main areas where HTML5 tried to improve?What are the main areas where HTML5 tried to improve?May 13, 2025 am 12:12 AM

HTML5aimedtoimprovewebdevelopmentinfourkeyareas:1)Multimediasupport,2)Semanticstructure,3)Formcapabilities,and4)Offlineandstorageoptions.1)HTML5introducedandelements,simplifyingmediaembeddingandenhancinguserexperience.2)Newsemanticelementslikeandimpr

See all articles

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 Article

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MantisBT

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.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft