2

I was just upgraded to Vue2.0, and not so familiar with it.

One change makes trouble for me, that the vue-router initialization [docs] is changed:

In my case, the initialization code is changed as below:

Old

import RootApp from './components/RootApp.vue';

router.start(RootApp, '#app')

New

import RootApp from './components/RootApp.vue';

new Vue({
  el: '#app',
  router: router,
  render: h => h(RootApp)
})

Soon I found that specifying the RootApp in such a manner of new case make the root app code ambiguous for me.

That in old case, the $root element of each sub-route element yields the RootApp instance, while in new case, it yields another component which contains the RootApp instance as its only child.

So, it makes trouble, is there any way to create the RootApp just acts the root node in Vue2?

Or I guess, is there any way to create an Vue instance like the below (but failed when tried):

# Failed code to tell what I want

import RootApp from './component/RootApp.vue';
new RootApp({
    el: '#app',
    router: router,
});

2 Answers 2

5

After a long time to test, I found the last attempt is almost there, see another question:

Vue.js 2: How to initialize(construct) a Vue component from a .vue file?

The following code worked perfectly!

import RootApp from './component/RootApp.vue';
const RootAppConstructor = Vue.extend(RootApp);
new RootAppConstructor({
    el: '#app',
    router: router,
});
Sign up to request clarification or add additional context in comments.

3 Comments

That's a nice way around refactoring. +1 - would also maintain your <style> tags which is really good
@GuyC what do you mean by maintain the <style> tag? Any question posted or something else?
no question - just if you'd used my answer you would of had to have worked with the CSS in a different manner, however with the method above you have a <style> tag within your .vue file which will allow you to set base styles for the whole app. Basically I like this idea - nice one!
1

You could refactor your RootApp so that it's methods / data etc... all reside within this new root Vue instance.

This will mean also copying across RootApp's template to your main index.html (or whatever you use) file. When done though everything should function as before, albeit not from a single .vue file.

2 Comments

Don't understand. I just want the redundant root component to be collapsed.
Sorry that's what I was saying, that you could refactor your current app by moving your methods, data etc.. into that new root instance - thus making it not redundant. As far as I'm aware you can't render a child component and then ask Vue to consider it the $root, and nor can you use a .vue file as the root of your app

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.