0

I want to switch between two single file component in vuejs, i don't how to use attach and detach functions

2

1 Answer 1

2

You can use the component which is a built in component provided by vue.

HTML

<div id="app">
  <h3>Parent component</h3>
  <button @click="current= 'ComponentA'">Show component A</button>
  <button @click="current= 'ComponentB'">Show component B</button>
  <component :is="current"></component>
</div>

script

const ComponentA ={
  template:"<h4>This is component A</h4>"
}

const ComponentB ={
  template:"<h4>This is component B</h4>"
}

new Vue({
  el:'#app',
  data:{
    current: "ComponentA"
  },
  components:{
    ComponentA,
    ComponentB
  },

})

Here is the fiddle

Sign up to request clarification or add additional context in comments.

Comments

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.