Home Web Front-end Front-end Q&A How to run vue files in the browser

How to run vue files in the browser

Sep 29, 2021 pm 04:57 PM
run

How to run the vue file in the browser: 1. Open the cmd command window and use the cd command to enter the vue project directory containing the vue file; 2. In the project directory, run the command "npm run dev" to start Project; 3. Enter "localhost:8080" in the browser address bar.

How to run vue files in the browser

The operating environment of this tutorial: windows7 system, vue2.9.6 version, DELL G3 computer.

The vue file is run in the browser

  • ##New vue file

The official example is as follows, you need to create the index.html file:

<html><body>
  <p id="app"></p>
  <script src="https://unpkg.com/vue@next"></script>
  <script src="https://cdn.jsdelivr.net/npm/vue3-sfc-loader/dist/vue3-sfc-loader.js"></script>
  <script>

    const options = {

      moduleCache: {
        vue: Vue      },

      async getFile(url) {

        const res = await fetch(url);
        if ( !res.ok )
          throw Object.assign(new Error(url+' '+res.statusText), { res });
        return await res.text();
      },

      addStyle(textContent) {

        const style = Object.assign(document.createElement('style'), { textContent });
        const ref = document.head.getElementsByTagName('style')[0] || null;
        document.head.insertBefore(style, ref);
      },
    }

    const { loadModule } = window['vue3-sfc-loader'];

    const app = Vue.createApp({
      components: {
        'my-component': Vue.defineAsyncComponent( () => loadModule('./myComponent.vue', options) )
      },
      template: '<my-component></my-component>'
    });

    app.mount('#app');

  </script></body></html>
Copy after login

Then you need to create the myComponent.vue file, the file content is as follows:


<template>
  <p class="title">
    hello world
  </p>
</template>

<script>
export default {
  setup () {
    console.log('hello world')
  }
}
</script>

<style scoped>
  .title {
    font-size: 40px;
    color: red;
  }
</style>
Copy after login

  • Start the project

In cmd, use the cd command to enter the vue project

In the project directory, run the command

npm run dev will run our application using hot loading. Hot loading allows us to see the modified effects in real time without manually refreshing the browser after modifying the code.

  • Enter localhost:8080 in the browser.

Related recommendations: "

vue.js Tutorial"

The above is the detailed content of How to run vue files in the browser. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to execute .sh file in Linux system? How to execute .sh file in Linux system? Mar 14, 2024 pm 06:42 PM

How to execute .sh file in Linux system?

PyCharm usage tutorial: guide you in detail to run the operation PyCharm usage tutorial: guide you in detail to run the operation Feb 26, 2024 pm 05:51 PM

PyCharm usage tutorial: guide you in detail to run the operation

Reasons why exe files cannot be run on Windows 7 Reasons why exe files cannot be run on Windows 7 Feb 18, 2024 pm 08:32 PM

Reasons why exe files cannot be run on Windows 7

Why can't I execute bat file on Windows 7? Why can't I execute bat file on Windows 7? Feb 19, 2024 pm 03:19 PM

Why can't I execute bat file on Windows 7?

How to run Javascript from Python? How to run Javascript from Python? Sep 07, 2023 pm 11:33 PM

How to run Javascript from Python?

How to run m-file in matlab - Tutorial on running m-file in matlab How to run m-file in matlab - Tutorial on running m-file in matlab Mar 04, 2024 pm 02:13 PM

How to run m-file in matlab - Tutorial on running m-file in matlab

Which win10 version runs the fastest? Which win10 version runs the fastest? Jan 05, 2024 pm 05:29 PM

Which win10 version runs the fastest?

Where to open win8 running Where to open win8 running Mar 20, 2024 pm 03:46 PM

Where to open win8 running

See all articles