Sample code using vue-router in html

Sample code using vue-router in html

Introducing vue and vue-router

<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>

Complete Example

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>
<body>
    <div id="app">
        <h1>Hello!</h1>
        <p>
          <!-- Use the router-link component to navigate. -->
          <!-- Specify the link by passing in the `to` attribute. -->
          <!-- <router-link> will be rendered as an `<a>` tag by default -->
          <router-link to="/hash1">Switch to com1</router-link>
          <router-link to="/hash2">Switch to com2</router-link>
        </p>
        <!-- Routing exit -->
        <!-- Components matched by the route will be rendered here-->
        <router-view></router-view>
        <!-- Other attributes on router-link: -->
        <!-- If the replace attribute is set, router.replace() will be called instead of router.push() when clicking.
            No history record is left after navigation. -->
        <!-- <router-link :to="{ path: '/abc'}" replace></router-link> -->
        <!-- Sometimes you want <router-link> to render into some kind of tag, such as <li>. So we use the tag prop class to specify the tag, and it will still listen for clicks and trigger navigation. -->
        <!-- <router-link to="/foo" tag="li">foo</router-link> -->
        <!-- active-class sets the CSS used when the link is activated -->
        <!-- event declares an event that can be used to trigger navigation. Can be a string or an array containing strings. -->
    </div>
</body>
<script>
    // 1. Define (routing) components.
    const com1 = { template: '<div>Route 1</div>' }
    const com2 = { template: '<div>Route 2</div>' }
    
    // 2. Define routes // Each route should map to a component. Where "component" can be extended through Vue.extend()
    // The component constructor to create, or just a component configuration object.
    const routes = [
        { path: '/hash1', component: com1 },
        { path: '/hash2', component: com2 }
    ]
    
    // 3. Create a router instance and pass `routes` configuration const router = new VueRouter({
        routes // (abbreviation) equivalent to routes: routes
    })
    
    // 4. Create and mount the root instance.
    // Inject routes through router configuration parameters so that the entire application has routing functionality const app = new Vue({
        router
    }).$mount('#app');//el is automatic mounting, mount is manual mounting (delay)
    
</script>
</html>

This is the end of this article about the sample code of using vue-router in html. For more relevant content about using vue-router in html, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

<<:  The front-end page pop-up mask prohibits page scrolling

>>:  Implementation of Single Div drawing techniques in CSS

Recommend

Share 12 commonly used Loaders in Webpack (Summary)

Table of contents Preface style-loader css-loader...

Tutorial on installing PHP on centos via yum

First, let me introduce how to install PHP on Cen...

Introduction to Linux File Compression and Packaging

1. Introduction to compression and packaging Comm...

vue3+ts+EsLint+Prettier standard code implementation

Table of contents use Use of EsLint Add a profile...

Tutorial on installing nginx in Linux environment

Table of contents 1. Install the required environ...

How to analyze MySQL query performance

Table of contents Slow query basics: optimizing d...

Sharing tips on using vue element and nuxt

1. Element time selection submission format conve...

getdata table table data join mysql method

public function json_product_list($where, $order)...

The whole process of IDEA integrating docker to deploy springboot project

Table of contents 1. IDEA downloads the docker pl...

Example code of how CSS matches multiple classes

CSS matches multiple classes The following HTML t...

How to create a MySQL master-slave database using Docker on MacOS

1. Pull the MySQL image Get the latest MySQL imag...

About MYSQL, you need to know the data types and operation tables

Data Types and Operations Data Table 1.1 MySQL ty...

Detailed explanation of common usage of pseudo-classes before and after in CSS3

The before/after pseudo-class is equivalent to in...