Exploring the integration of Ant Design Vue within HTML code

How can ant design vue be implemented in HTML?

I attempted to install this library using npm and obtained the following files: antd.js, antd-with-locales.js.map, antd.js.map, antd-with-locales.min.js, antd.min.js, antd-with-locales.min.js.LICENSE.txt, antd.min.js.LICENSE.txt, antd-with-locales.min.js.map, antd.min.js.map, reset.css, antd-with-locales.js. I tried including them in my code within script tags, but encountered an error in the console. This is the code snippet:

<!DOCTYPE html>
<html>

<head>
    <link rel="stylesheet" href="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="177679633a7372647e70793a616272572339273924">[email protected]</a>/dist/reset.css">
    <script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f5838090b5c6dbc6dbc1">[email protected]</a>/dist/vue.global.js"></script>
    <!-- Other dayjs scripts -->
    <title>My first ant design vue page</title>
</head>

<body>
    <template>
        <a-button>Add</a-button>
    </template>
    <script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="83e2edf7aee7e6f0eae4edaef5f6e6c3b7adb3adb0">[email protected]</a>/dist/antd.min.js"></script>
    <script>
        import { Button } from 'ant-design-vue';
        const ButtonGroup = Button.Group;

        export default {
            components: {
                AButton: Button,
                AButtonGroup: ButtonGroup,
            },
        };
    </script>
</body>

</html>

This is the error message I received:

Uncaught SyntaxError: Cannot use import statement outside a module (at first.html:24:9)

Answer №1

If you're attempting to utilize it as a Single File Component (*.vue), bear in mind that this necessitates a build step to make files browser-ready.

To directly use the library in the browser, ensure to make use of the global antd variable.

const { Button } = antd;
const ButtonGroup = Button.Group;

Vue.createApp({
  components: {
    AButton: Button,
    AButtonGroup: ButtonGroup,
  },
}).mount("#app");
<script src="https://unpkg.com/dayjs/dayjs.min.js"></script>
<script src="https://unpkg.com/dayjs/plugin/customParseFormat.js"></script>
<script src="https://unpkg.com/dayjs/plugin/weekday.js"></script>
<script src="https://unpkg.com/dayjs/plugin/localeData.js"></script>
<script src="https://unpkg.com/dayjs/plugin/weekOfYear.js"></script>
<script src="https://unpkg.com/dayjs/plugin/weekYear.js"></script>
<script src="https://unpkg.com/dayjs/plugin/advancedFormat.js"></script>
<script src="https://unpkg.com/dayjs/plugin/quarterOfYear.js"></script>
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f88e8d9db8cbd6cbd6cc">[email protected]</a>/dist/vue.global.js"></script>
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5f3e312b723b3a2c36383172292a3a1f6b716f716c">[email protected]</a>/dist/antd.min.js"></script>
<link rel="stylesheet" href="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="40212e346d24253329272e6d36352500746e706e73">[email protected]</a>/dist/reset.css">
<div id="app">
  <a-button>Add</a-button>
</div>

A more streamlined approach is to extract all components from the antd variable instead of manually including them. Additionally, consider registering them globally for easy access in your custom components without individual imports.

const app = Vue.createApp({
  data: () => ({
    "text": "",
  }),
});
Object.values(antd).forEach(o => {
  if (typeof o == "object" && o.name) {
    app.component(o.name, o);
  }
});
app.mount("#app");
<script src="https://unpkg.com/dayjs/dayjs.min.js"></script>
<script src="https://unpkg.com/dayjs/plugin/customParseFormat.js"></script>
<script src="https://unpkg.com/dayjs/plugin/weekday.js"></script>
<script src="https://unpkg.com/dayjs/plugin/localeData.js"></script>
<script src="https://unpkg.com/dayjs/plugin/weekOfYear.js"></script>
<script src="https://unpkg.com/dayjs/plugin/weekYear.js"></script>
<script src="https://unpkg.com/dayjs/plugin/advancedFormat.js"></script>
<script src="https://unpkg.com/dayjs/plugin/quarterOfYear.js"></script>
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6315160623504d504d57">[email protected]</a>/dist/vue.global.js"></script>
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="462728326b2223352f21286b303323067268766875">[email protected]</a>/dist/antd.min.js"></script>
<link rel="stylesheet" href="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5d3c33297039382e343a33702b28381d69736d736e">[email protected]</a>/dist/reset.css">
<div id="app">
  <a-button-group>
    <a-button @click="text = 'A'">A</a-button>
    <a-button @click="text = 'B'">B</a-button>
    <a-button @click="text = 'C'">C</a-button>
  </a-button-group>
  <a-alert v-if="text" show-icon :message="text"></a-alert>
</div>

If you're serious about working with Vue (and Ant Design), explore how to leverage *.vue files with vite.

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

Converting Cyrillic characters to ASCII codes using JavaScript: A step-by-step guide

Is there a reliable method to convert characters from the CP1251 table to ASCII codes ranging from 0 to 255? So far, I have only come across the charCodeAt() function which is limited to codes up to 128. It returns a Unicode number for codes above that r ...

jQuery fails to operate on products loaded through AJAX requests

Upon opening the page, jQuery functions correctly. However, when a product is loaded or changed via AJAX, jQuery stops working. I am currently using jquery-1.7.1.min.js $(document).ready(function () { $screensize = $(window).width(); if ($screensi ...

Saving solely the content of an HTML list element in a JavaScript array, excluding the image source

One issue I am facing is with an unordered list in HTML which contains items like <ul id="sortable"> <li class="ui-state-default"><img src="images/john.jpg">John</li> <li class="ui-state-default"><img src="images/lisa.jpg ...

tips for effectively utilizing getters in array sorting operations

I've been encountering some issues with vuex getters. My objective is to arrange the cart data, which consists of an array of objects, by date using the getter named myCartItems. The problem I'm facing is that when I add a second payload {prod_ ...

Looking to automatically scroll to the bottom by clicking on text using javascript/jquery?

I am currently working on a website project where I have a specific requirement. I need the webpage to scroll towards the bottom when a particular text is clicked, using JavaScript/jQuery. <p class="ml-2 mb-2 d-flex view_profile_options"><a hre ...

The error "Call to a member function exports() on array" occurs when attempting to use the

As I attempt to send an array of values to the jobExport() collection, I encounter an error stating Call to a member function jobsExport() on array. I comprehend the necessity for the collection to be populated with modal collection value. However, my goal ...

generate an object with the forEach method

When I receive data from a graphql query, my goal is to structure an object like the example below: const MY_DATA = [ { id: 'bd7acbea-c1b1-46c2-aed5-3ad53abb28ba', imageUrl: defaultUrl, name: 'Johann', } ...

Using JavaScript to show content in a textarea field

In my index.jsp file, I have implemented the following code to populate two textareas, INPUT_TEXT and INPUT_TEXT2, with processed data. This involves passing the text through a servlet, a Java class, and finally displaying the preprocessed results in the s ...

If Gulp is running continuously, what output will I receive?

After reading a helpful post on StackOverflow about gulp tasks (viewable here), I came to the conclusion that it's not advisable to omit returning anything in a gulp task. For proper synchronization of asynchronous tasks, the caller should wait. Pres ...

I am currently studying JavaScript. The syntax of my if statement with && appears to be accurate, however

I'm having trouble with the logic in my "Code your Own Adventure" program on Code Academy. I expect it to display "Great, come get your pizza!" when I enter PIZZA, YES, and YES as prompts, but instead it says "NO pizza for you!" I've tried using ...

Converting an object to JSON in javascript: A step-by-step guide

I have been attempting to convert my object person into a JSON format. const person = new Object(); person.firstName = 'testFirstName'; person.lastName = 'testLastName'; var myJson = JSON.stringify(person); ...

Having trouble with updating links in HTML pages?

I'm having trouble updating all the links in my HTML file. The code I have isn't working as expected. var fs = require('fs'); fs.readFile(__dirname + '/index.html', 'utf8', function(err, html){ if(!err){ html = ...

Getting data from an Ajax call

I am struggling to find a solution for retrieving data from a processing script into a Request page using Ajax and PHP. The issue I am facing is shown in the error message below: SyntaxError: JSON.parse: unexpected character at line 1 column 13 of the J ...

Is there a way to save a Morris.js chart as a PDF file

I have successfully created a Morris.js Bar Chart using data from PHP and MySQL. Now, I am looking for a way to export this chart into a PDF format. I have attempted to do so using the FPDF library but I am struggling with the implementation. Can anyone ...

What is the importance of using ChangeDetectorRef.detectChanges() in Angular when integrating with Stripe?

Currently learning about integrating stripe elements with Angular and I'm intrigued by the use of the onChange method that calls detectChanges() at the end. The onChange function acts as an event listener for the stripe card, checking for errors upon ...

challenges with Next.js dynamic routing when dealing with spaces

I am facing an issue with accessing dynamic routes using a product name from my API when the name contains spaces. However, I can access the routes without any problem if the product name is just a single word. utils/api.js export async function getProduc ...

Retrieving User Activity Reports for a specified set of users within G Suite

I am currently attempting to utilize the Admin SDK Reports Service to retrieve the latest login time and other data for a specific set of 20 users. Due to the large size of the domain, it is not practical to fetch data for the entire domain and then filter ...

Searching for MongoDB / Mongoose - Using FindOneById with specific conditions to match the value of an array inside an object nestled within another array

Although the title of this question may be lengthy, I trust you grasp my meaning with an example. This represents my MongoDB structure: { "_id":{ "$oid":"62408e6bec1c0f7a413c093a" }, "visitors":[ { "firstSource":"12 ...

Fullcalendar inaccurately displays events

I recently integrated fullcalendar into my website, but I've come across a strange bug. The event is set up correctly with all the necessary information, yet when the calendar loads, it appears in the wrong position and for the wrong duration. However ...

Using Array.Map() to retrieve the child array within a React component

Here is the data I have retrieved from an API: [ { "id": 1, "appName": "string", "defaultAction": "string", "defaultMessage": "string", "rules": [ { "id": 1, "version": "string", "brand": "string", ...