0

It’s possible to use same part of template in many places without duplicate code? I know that in JSX I can put the part of template into function and render that function in many places, but it’s possible in vue template ?

for example

<template>
    <element-to-use>
      <h1>{{ title }}</h1>
      <p>{{ description }}</p>
    </element-to-use>

    <tooltip>
      <template #content>
        <element-to-use></element-to-use>
      </template>

      <div>
        <img src="/img.png" />
        <element-to-use></element-to-use>
      </div>
    </tooltip>
  </template>
3
  • In your case is element-to-use ? Commented Nov 23, 2021 at 11:19
  • @Mateusz Ciołko That is what the Vue core feature of abstraction into re-usable Vue components is for, preventing you from code duplication aswell. Commented Nov 27, 2021 at 12:30
  • In Angular you actually can do it like above with <ng-template #name>...</ng-template> and then you can place it like this <ng-container *ngTemplateOutlet="name" />. Too bad Vue doesnt have it. Commented Feb 28, 2024 at 14:45

1 Answer 1

1

you can always create a vue component and put what ever you want inside it, then re-use that component anywhere you want. its just like what you said about react. just create a file like 'myComponent.vue', put your code inside it, then where ever you want to use it, just import it and define it as a component for the component you want to use it in. or you can define it as a global component.

for example, you can use it like this:

import myComponent from './components/myComponent.vue'
app.component('my-component', myComponent );

this way its a global component and you can place

<my-component></my-component>

anywhere

or you can import it and use it inside an specific component:

import myComponent from './components/myComponent.vue'
export default {
    data(){
     return{}
    },
    methods:{},
    components: {
       myComponent,
    },
}

and use it inside that component only :

<myComponent></myComponent>
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, this is very corrrect, just take advantage of vue's components.
I know that I can create components but putting part of simple HTML into components is a good practice? If that part has some logic - okay - but if it's static HTML You will have more code by creating a new component then copy/paste part of the template
one of the best things about a component is that you can use it 100 times and when you want to change anything about it, you just change that one component. now, imagine copy/pasting the same html 100 times, and all of a sudden you need to change that little html, what are you going to do? change all 100? no. yes its logical to create a component for static html if you want to use it many times.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.