6

Consider this simplified example of a real-world app:

<html>
  <body>
    <script src="https://unpkg.com/vue"></script>
    <div id="app">
      <div id="subapp">
        <p>
          {{ message}}
        </p>
      </div>
      <p>{{ message }}</p>
    </div>

    <script>
      new Vue({
        el: '#app',
        data: {
          message: 'Hello Vue.js!'
        }
      })

      new Vue({
        el: '#subapp',
        data: {
          message: '¡Hola Vue.js!'
        }
      })

    </script>
  </body>
</html>

I'd expect to see two different messages, but i get the same message twice. If i change message to other_message in the 'subapp', Vuejs complains that it can not find other_message.

Is there a way to "embed" them together? Short of un-embedding the html part or merging the controllers, is there a way of getting the expected result? Alternatively, is there a better term for what i'm trying to accomplish? (english is hard sometimes)

1
  • 1
    You could use components. Define a component (contains the controller and the view) and you can use that component twice. But a better answer could be given if you could give more details though Commented Dec 12, 2017 at 13:02

3 Answers 3

8

Like @ka_lin has said, you should use components for this. They are the perfect tools for this.

Otherwise it is almost impossible to do, specially with the example you have presented. There is no way Vuejs can recognize to which instance {{ message }} belongs to.

The closest you can achive a 'similar' feature is to distribute the scope of your vue instance to two elements as:

new Vue({
 el: '#app1',
 data () {
   return {
     message: 'Hello'
   }
 },
})

new Vue({
 el: '#app2',
 data () {
   return {
     message: 'Helloa'
   }
 }
})
<script src="https://unpkg.com/[email protected]/dist/vue.js"></script>

<div id="app1">
  {{ message }}
</div>

<div id="app2">
  {{ message }}
</div>

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

3 Comments

What if we don't know in advance, how many <div id="appN"> will be?
@GurebuBokofu I suppose you could use getElementsById with some regex to capture all the divs and use foreach loop. But I wouldn't do that. I haven't been using vue for a year, so you should better ask it in the main site
You could easily make a mount fn and give the id. Then you can just call that every time
1

Try such an example:-

  const App = new Vue({
    el: '#app',
    data: {
      aa: 'Hello Vue!', }

  })
  const vvv2 = new Vue({
    el: '#vvv',
    data: {
      bb: 'Hello Vue!55555555555', }

  })

Comments

0

In Vue3 you aren't limited of app instances. You can create unlimited instances.

const app1 = createApp({
  /* ... */
})
app1.mount('#container-1')

const app2 = createApp({
  /* ... */
})
app2.mount('#container-2')

https://vuejs.org/guide/essentials/application.html#multiple-application-instances

1 Comment

How to tell composables apart?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.