Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue.js loading single-file components

Tags:

I'm new to Vue.js and want to use single-file components, but I don't understand the workflow.

For example, I have three components: App, Grid and List

App.vue

<template>     <div id="app">         <div id="grid"></div>         <div id="right"></div>     </div> </template>  <script>     export default {         name: 'app',         data () {             return {                 message: 'Hello Vue!'             }         }     } </script> 

Grid.vue

<template>     <div id="left"></div> </template>  <script>     export default {         name: 'grid',         data: function () {             return {                 grid: 'some-data'             }         }     } </script> 

List.vue

<template>     <div id="right"></div> </template>  <script>     export default {     name: 'list',     data: function () {         return {             text: 'some-text'         }     } } </script> 

Main.js

import Vue from 'vue' import App from './vue/App.vue' import Grid from './vue/Grid.vue' import PatternList from './vue/PatternList.vue'  new Vue({     el: '#app',     render: h => h(App) });  new Vue({     el: '#grid',     render: h => h(Grid) });  new Vue({     el: '#right',     render: h => h(PatternList) }); 

It works but I hope this isn't the right way to create nested components.

Can anyone show the way it should be done? Thanks

like image 278
Dmitry Avatar asked Jun 15 '17 13:06

Dmitry


People also ask

What is single file components in Vue?

Vue Single-File Components (a.k.a. *.vue files, abbreviated as SFC) is a special file format that allows us to encapsulate the template, logic, and styling of a Vue component in a single file. Here's an example SFC: vue <script> export default { data() { return { greeting: 'Hello World!'

How do I load a Vue js file?

There are two ways to import a JavaScript library to the Vue Component. The first is to import a local JavaScript library. Here, you can import the JavaScript library by using the 'import' keyword inside the script tag of your Vue file. import * as mykey from '../assets/js/mykey.

What is a single file component?

Single File Components(SFCs) are a style of application organization used by JavaScript UI libraries where each file represents a single component in all aspects. Typically they resemble an HTML document where you have HTML tags, Style Tag, and Script Tag all in a file.

How do I import components to Vue?

STEP 01: First, Import the Child Component into the Parent Component inside script tag but above export default function declaration. STEP 02: Then, Register the Child Component inside the Parent Component by adding it to components object. STEP 03: Finally, Use the Child Component in the Parent Component Template.


1 Answers

You can register components using the Vue.component method:

import Vue from 'vue' import App from './vue/App.vue' import Grid from './vue/Grid.vue' import PatternList from './vue/PatternList.vue'  Vue.component('grid', Grid); Vue.component('pattern-list', PatternList);  new Vue({   el: '#app',   render: h => h(App) }); 

And then add them to the App template directly using their tag name:

<template>   <div id="app">     <grid></grid>     <pattern-list></pattern-list>   </div> </template> 

This registers the components globally, meaning that any Vue instance can add those components to their templates without any additional setup.


You can also register components to a Vue instance, like so:

new Vue({   el: '#app',   render: h => h(App),   components: {     'grid': Grid,     'pattern-list': PatternList   } }); 

Or within the script tag of a single-file component:

<script> import Grid from './vue/Grid.vue'  export default {   name: 'app',   components: {     'grid': Grid,     'pattern-list': PatternList   } }); </script> 

This registers the components just to that Vue instance, meaning that those registered components will only be available to use within the template for that Vue instance. A child component would not have access to those registered components unless those components are also registered to the child Vue instance.

like image 103
thanksd Avatar answered Sep 22 '22 13:09

thanksd