struggling to utilize vue.js 2 components

I just started learning vue.js from the book titled "Getting to know Vue". However, I am facing some challenges with the components page. The sample code provided in the book is not working for me and every time I try to open the HTML file, it just shows a blank page.

Despite searching on Google, Yahoo, and even on the official Vue documentation, none of the examples seem to work for me. It's strange that I am unable to execute this code properly. Any assistance in getting this code to work would be greatly appreciated.

Below is the code snippet:
The version of vue.js being used is 2.7.14

<!doctype html>
<html>
    <head></head>    
    <body>    
      <script src="vue.js"></script>
      <script>
        Vue.component('our-header', {
          template: `<h1>App Header</h1>`
        });
      </script>    
      <div id='our-header'>
        <our-header></our-header>
      </div>
    </body>
</html>

Answer №1

When your Vue instance is in the "unmounted" state, it means there is no associated DOM element. You can use vm.$mount() to manually initiate the mounting process for an unmounted Vue instance.

<!doctype html>
<html>
    <head></head>    
    <body>    
      <div id='app'>  
      </div>
      <script src="vue.js"></script>
      <script>
        const OurHeader = Vue.component('our-header', {
          template: `<h1>App Header</h1>`
        });
        var app = new Vue({
          render: (h) => h(OurHeader),
        }).$mount('#app')
        
      </script> 
    </body>
</html>

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.7.14/vue.js"></script>
<!doctype html>
<html>
    <head></head>    
    <body>    
      <div id='app'>  
      </div>
      <script>
        const OurHeader = Vue.component('our-header', {
          template: `<h1>App Header</h1>`
        });
        var app = new Vue({
          render: (h) => h(OurHeader),
        }).$mount('#app')
      </script> 
    </body>
</html>

Answer №2

Consider making a change

<script src="vue.js"></script>

Try updating it to

<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

To test if it will work. It seems like there might have been an issue with importing VueJS properly.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Passing data between Vue components

Currently, I am extracting a language setting from the DOM and assigning it to a global Vue.js variable in the following manner: const language = document.querySelector('html').getAttribute('lang'); Vue.prototype.$language = language; ...

Managing query inputs in React and Express applications

Within my React application, I utilized a REST API to make a request to the server. In the URL, I intended to include a query using the postIconsTransition method. However, when I try to send this request, the server responds with an error message stating ...

Retrieving data from MongoDB and presenting it neatly in Bootstrap cards

I have successfully retrieved data from MongoDB and displayed it in Bootstrap 5 cards. However, I am facing an issue where all the cards are appearing in a single row if there are multiple entries in the database. What I want to achieve is to utilize the ...

Preventing conflicts while toggling visibility with JQuery across various groups

My navigation system allows me to show/hide divs and toggle an 'active' class on the navigation items. However, I've encountered conflicts when trying to use similar sections on the same page (clicking a link in one section affects divs in o ...

Issue encountered while selecting the Electron app menu item

I encountered an error when clicking on a menu item: Any suggestions on how to resolve this issue? Here is the snippet of code that seems to be causing the problem: Main.js const { Menu } = require('electron') const { createNewFile } = require ...

Leveraging the NextAuth hooks, employ the useSession() function within the getServerSideProps

I'm currently working on retrieving data from my server based on the user who is logged in. I am utilizing Next-Auth and usually, I can easily obtain the session by calling: const { data: session } = useSession(); In a functional component, this work ...

What are some methods to make sure that functions in AngularJS do not run at the same time

I am new to the world of JavaScript and I have encountered a problem with one of my functions. The function is designed to retrieve cached resources, but if the resource is not found in the cache, it makes a call to the back-end. The issue arises when thi ...

Dynamically showcasing content in an HTML table

Having trouble with this code snippet. It's supposed to iterate through array objects and display them in an HTML table. The third row should have buttons, but nothing is showing up. Can you spot the issue? Here's the HTML code: <html> & ...

Adjust the text according to the selected checkbox option

I am attempting to update the displayed text based on whether a checkbox is currently checked or not. The value of the viewable text should reflect the user's selection. I believe I am close, but the functionality is not working as expected. <html ...

Executing php class method through ajax with jQuery without requiring a php handler file

How can I trigger a PHP class method using AJAX with jQuery without the need for a separate PHP handler file? Here is my PHP Class animal.php:- <?php class animal { function getName() { return "lion"; } } ?> jQuery code snippet:- ...

Create a unique custom spinning loader for a straightforward Vue.js application

I've set up a basic Vue project on codesandbox.io that includes vue-router and a custom spinner for loading. Spinner.vue: <template> <div class="spinner" v-show="loading"> <span class="sync" v-bind:style="[spinnerStyle, spinnerD ...

Having trouble running my Vue project due to an issue in the app.scss file

Attempting to launch a Vue project locally, I executed the following command: npm run dev An error message was displayed as follows: error in ./src/assets/scss/app.scss Module build failed: Error: ENOENT: no such file or directory, scandir '/Use ...

Dropped down list failing to display JSON data

I created a website where users can easily update their wifi passwords. The important information is stored in the file data/data.json, which includes details like room number, AP name, and password. [ {"Room": "room 1", "AP nam ...

Unknown Parameters Issue with Vue.js Router Links

Within my Vue.js project, I am utilizing params in my navigation.vue component to pass data onto the next page for dynamic routing purposes. Below is an example of how I am using this: <router-link tag="p" :to="{name: 'Main', ...

No input values were passed to express-validator

In my node app, I am using express-validator and encountering validation errors for all 4 form fields ("A name is required" and so on). Strangely, when I console.log the errors variable, all values are blank: errors: [{value: '', msg: 'A na ...

Divide the promised variable into separate named variables

Here is a code snippet that I am working with: Promise.all( categories.map(category => Collection.find({ category })) ).then(items => { return items }) Currently, the output is an array with the same length as categories, where each element is ...

JQuery is capable of hiding a div using the .hide() method but is unable to reveal it using the

I am currently working on a basic webpage that includes a question and a set of radio buttons. Depending on the option selected by the user, the question will be hidden and either a correct or incorrect message will be displayed, both of which are initiall ...

React Ant Design: Toggle Visibility of Columns

Seeking a method to toggle the visibility of columns for the Table Component in Ant Design. The goal is to include a checkbox next to each column name. When unchecked, the column should be hidden. In my previous experience with react-table, accomplishing ...

Combining the powers of Socket.io, node.js, and smart routing

How can I pass an already connected socket from socket.io to the controller when making a post request from the application to the server, using Vue and a Node server with Socket.io? I attempted declaring global.io = io, but the socket remains undefined. ...

Can you provide an explanation of what the 'attribution' key does in the tomTomMap.addSource() function?

I came across a solution on how to integrate the openweathermap layer into a tom-tom map: tomTomMap.addSource('owm_source', { type: 'raster', tiles: [ 'https://tile.openweathermap.org/map/clouds_new/{1}/{51}/{20}.png? ...