Home  >  Article  >  Web Front-end  >  Vite creates Vue3 projects and how Vue3 uses jsx

Vite creates Vue3 projects and how Vue3 uses jsx

PHPz
PHPzforward
2023-05-22 13:58:202308browse

    Vite creates a Vue3 project

    Vite requires Node.js version >= 12.0.0. (node -v Check your current node version)

    • Use yarn: yarn create @vitejs/app

    • Use npm: npm init @vitejs/app

    1. Enter the project name

    Enter us here Project name: vite-vue3

    Vite creates Vue3 projects and how Vue3 uses jsx

    2. Select the framework

    Here select the framework we need to integrate: vue

    Vite creates Vue3 projects and how Vue3 uses jsx

    • vanilla: native js, without any framework integration

    • vue: vue3 framework, Only supports vue3

    • react: react framework

    • preact: lightweight react framework

    • lit -element: lightweight web component

    • svelte: svelte framework

    3. Choose a different vue

    Here we Select: vue

    Vite creates Vue3 projects and how Vue3 uses jsx

    4. Project creation completed

    Vite creates Vue3 projects and how Vue3 uses jsx

    5. Project structure

    The project structure is very simple:

    Vite creates Vue3 projects and how Vue3 uses jsx

    6. Start the project

    • Enter the project:cd vite-vue3

    • Install dependencies: npm install

    • ##Run the project:

      npm run dev Or npx vite

    • ##Compile project:
    • npm run build

      or npx vite build

    • Startup speed
    Extremely fast

    :

    Vite creates Vue3 projects and how Vue3 uses jsx

    ##Using jsx in Vue3Vite creates Vue3 projects and how Vue3 uses jsx

    jsx cannot be used directly in the Vue3 project created by Vite. You need to introduce a plug-in to achieve it

    1. Install the plug-in

    Use yarn:
      yarn add @vitejs /plugin-vue-jsx -D
    • Use npm:
    • npm i @vitejs/plugin-vue-jsx -D
    • 2. Register the plug-in
    vite.config.js:

    import { defineConfig } from 'vite'
    import vue from '@vitejs/plugin-vue'
    import vueJsx from "@vitejs/plugin-vue-jsx";
    
    // https://vitejs.dev/config/
    export default defineConfig({
        plugins: [vue(), vueJsx()]
    })

    3. Use the plug-in

    Method 1: Modify

    App. vue

    Without jsx, App.vue

    looks like this:

    <script setup>
    import HelloWorld from &#39;./components/HelloWorld.vue&#39;;
    </script>
    
    <template>
        <img alt="Vue logo" src="./assets/logo.png" />
        <HelloWorld msg="Hello Vue 3 + Vite" />
    </template>
    Using jsx, App.vue looks like this:
    <script lang="jsx">
    import { defineComponent } from &#39;vue&#39;;
    import HelloWorld from &#39;./components/HelloWorld.vue&#39;;
    import logo from &#39;./assets/logo.png&#39;;
    
    export default defineComponent({
        render: () => (
            <div>
                <img alt="Vue logo" src={logo} />
                <HelloWorld msg="Hello Vue 3 + Vite" />
            </div>
        ),
    });
    </script>

    Method 2: Delete App.vue and create a new App.jsx

    Create a new App.jsx file

    import { defineComponent } from &#39;vue&#39;;
    import HelloWorld from &#39;./components/HelloWorld.vue&#39;;
    import logo from &#39;./assets/logo.png&#39;;
    
    export default defineComponent({
        setup () {
            return () => {
                return (
                    <div>
                        <img alt="Vue logo" src={logo} />
                        <HelloWorld msg="Hello Vue 3 + Vite" />
                    </div>
                )
            }
        }
    });

    Modify the introduction of main.js

    import App from './App.vue' changed to import App from './App'

    import { createApp } from &#39;vue&#39;
    import App from &#39;./App&#39;
    
    createApp(App).mount(&#39;#app&#39;)

    Note

    Does not support eslint when saving, do eslint Verification
    • Different from Webpack, Vite’s compilation entry is not a Javascript file, but index.html is used as the compilation entry. In index.html, main.js is loaded through . At this time, the request reaches the service layer of Vite

    The above is the detailed content of Vite creates Vue3 projects and how Vue3 uses jsx. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete