0

I am trying to learn Vue.JS, and the component I have made is not rendering on the page. This is my component:

import Vue from 'vue'

const card = new Vue({
  el: '#card',
  data: {
    title: 'Dinosaurs',
    content: '<strong>Dinosaurs</strong> are a diverse group of animals 
from the clade <em>Dinosauria</em> that first appeared at the Triassic 
period.'
  }
})

This is my html:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Vue Practice</title>
  </head>
  <body>
    <div id="card">
      <header>{{ title }}</header>
      <div v-html="content"></div>
    </div>
    <script src="bundle.js"></script>
  </body>
</html>

And this is package.json (I realize most of the dependencies belong in devDependencies):

    {
      "name": "vue-practice",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "bundle": "browserify -t babelify -t vueify -e main.js > 
                   bundle.js",
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "keywords": [],
      "author": "",
      "license": "ISC",
      "dependencies": {
        "babel-plugin-transform-es2015-modules-commonjs": "^6.26.0",
        "babelify": "^7.3.0",
        "browserify": "^14.4.0",
        "vue": "^2.4.2",
        "vueify": "^9.4.1"
      },
      "devDependencies": {}
    }

When I load index.html in the browser, all it renders is {{ title }} and I am receiving no errors. Any explanation as to why this is happening would be appreciated!

2
  • Are you using the Vue+Compiler package? Post your package.json. Commented Aug 30, 2017 at 19:54
  • I thought vueify would do the job, but I'll post the entire package.json. Commented Aug 30, 2017 at 19:57

1 Answer 1

2

vueify only transforms *.vue files, if you are going to use templating in your index.html then you need Vue compiler in order to compile the template in the browser (https://v2.vuejs.org/v2/guide/installation.html#Runtime-Compiler-vs-Runtime-only).

A good way to have things like that covered is to use a vuejs-template, if you are using Browserify there is one: https://github.com/vuejs-templates/browserify

But as you have almost everything up, you can only add what's missing for the compiler package, as stated in "Runtime + Compiler vs. Runtime-only" - "Browserify" section in Vue guide.

Add to your package.json:

{
    "browser": {
        "vue": "vue/dist/vue.common.js"
    },
}

That will tell Browserify to use the full (also called standalone) Vue build on browser.


Another way is to not use templates in index.html but instead in a main component, like App.vue, for that you check out the template I linked above, it does it out of the box.

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.