What is the best approach to incorporating Ant-design-vue via cdn in my project?

I've been working on a Vue macro application for specific functionality in NetSuite. Since I can't utilize npm or other package installers, I've resorted to using CDN. The Vue app and Ant Design are both functioning properly, but the issue lies in not picking up styles for any Ant Design components. Here's my code snippet:

<html>

<head>
  <title>Auto Refresh Page</title>
  <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fe888b9bbecdd0ccd0cdcf">[email protected]</a>"></script>
  <link href="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8dece3f9a0e9e8fee4eae3a0fbf8e8cdbea3bfa3bfbd">[email protected]</a>/dist/antd.min.css" rel="stylesheet">
  </link>
  <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>
</head>

<body>
  <div id="app">
  </div>
  <script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5e3f302a733a3b2d37393073282b3b1e6d706c706c6e">[email protected]</a>/dist/antd.min.js"></script>
  <script>
    const app = Vue.createApp({
      el: "#app",
      data() {
        return {
          moduleLoaded: false,
          currentRecordModule: null,
          backgroundProcess: false,
          showModal: false,
        };
      },
      methods: {
        init() {
          const currentRecordModule = require(['N/currentRecord'], (currentRecord) => {
            this.moduleLoaded = true
            this.currentRecordModule = currentRecord
          });
        },

        reloadPage() {
          location.reload(true)
        },

        showAlert() {
          this.showModal = true
          const iframeHtml = '<iframe src="${activeBgTaskUrl}" style="width: 100%; height: 400px; border: none;"></iframe>';
          
        },

        handleShowBackgroundProcessAlert(event) {
          if (event.target.id === 'showBackgroundProcessAlert') {
            this.showAlert()
          }
        },
      },

      mounted() {
        console.log('App mounted')
        this.init();
        document.addEventListener('click', this.handleShowBackgroundProcessAlert)
      },

      },

      template: `<a-button icon="search" type="primary">Primary Button</a-button>
    <a-modal v-model:visible="showModal" title="Basic Modal" @ok="handleOk">
      <p>Some contents...</p>
      <p>Some contents...</p>
      <p>Some contents...</p>
    </a-modal>`
    });
    app.mount('#app');
  </script>
</body>

</html>

My goal is to trigger a modal in the showAlert function. Despite trying various approaches like direct access to the Ant Design global variable, utilizing the import statement, and the require method, none of them seem to work. The button displays without proper styling, as does the modal. Given that creating a Suitelet in NetSuite is not feasible.

EDIT

The ant design components aren't loading and issuing me these warnings:

[Vue warn]: Failed to resolve component: a-modal
If this is a native custom element, make sure to exclude it from component resolution via compilerOptions.isCustomElement.
[Vue warn]: Failed to resolve component: a-button
If this is a native custom element, make sure to exclude it from component resolution via compilerOptions.isCustomElement.

Edit-2

Upon inspecting my source in the browser, it appears like this:

https://i.sstatic.net/A3ptW.png

While I can access Vue, accessing antd seems to be problematic. Any insights into why?

Answer №1

The Ant Design Vue (antdv) documentation includes a helpful tutorial on how to incorporate it into the browser:

Incorporating in Browser

To do so, you can insert script and link tags into your browser and utilize the global variable antd.


It is crucial to include the global variable antd in app.use().

Here's an example:

app.use(antd).mount('#app');

Answer №2

Expanding on the response from @saravanan-nandhan, it is essential to include an additional components parameter in the createApp function for specifying elements.

const { createApp } = Vue;

createApp({
  components: {
    AButton: window['antd'].Button,
    AModal: window['antd'].Modal,
  },
  ...
}).mount('#app');

Answer №3

After troubleshooting, I finally found a solution to the problem at hand. It turns out that the issue stemmed from version compatibility conflicts between Vue and Ant Design, as well as differences in the script providers I was using. Previously, I had been utilizing CDNJS for Vue and UNPKG for Ant Design. However, after making the switch to CDNJS for both libraries, everything fell into place. Below is an excerpt of the final script configuration:

  <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6a1c1f0f2a59445844595b">[email protected]</a>"></script>
  <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://cdnjs.cloudflare.com/ajax/libs/ant-design-vue/2.2.8/antd.min.js"></script>
  <link href="https://cdnjs.cloudflare.com/ajax/libs/ant-design-vue/2.2.8/antd.min.css" rel="stylesheet">

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

Unable to dynamically validate the input field for the name using Angular.js

Can someone assist me with an issue I'm having? I'm encountering an error while trying to dynamically validate input fields using Angular.js. Error: TypeError: Cannot read property '$invalid' of undefined Let me explain the code t ...

Is it possible for the useUser() function within the Auth0 nextjs-auth0 library to retrieve user information without relying on cookie data?

The useUser() method by Auth0 is designed to retrieve information about a logged-in user by calling the /api/auth/me endpoint. This triggers the handleAuth() function, which sets up Auth0 (creating a sessionCache instance, etc.) and calls profileHandler(re ...

Exploring the process of passing an array as a function argument from PHP to JavaScript

I am looking for assistance in passing an array as a function argument from PHP to JS. The values I need are retrieved from a database. while ($rows = pg_fetch_array($qry)) { ?> <option value="<?php echo $rows[&ap ...

What is causing my component callback to not function properly?

I'm currently following a tutorial on AngularJS callbacks from . In my code at https://plnkr.co/edit/dxyI0p0pZFZdMalLfvXO?p=preview, I've attempted to showcase the issue I'm encountering. I have initialized the component in three different w ...

Developing a unique attribute using AngularJS

As a beginner in AngularJS, I am experimenting with creating directives to manipulate the background-color of a <div> based on specific conditions. I aim to write code like this within my view: <div effect-color="#2D2F2A">content here</div& ...

retrieve the value of a text form using jQuery and its corresponding id

I need help with a basic HTML form that includes jQuery. My goal is to: 1) Retrieve the value into a JavaScript variable. 2) Send it as JSON to a server. Here is the form: <div data-role="main" class="ui-content"> <data=role "form" ...

Is it necessary to always provide a return value from a redux middleware function?

After reviewing the various examples provided in the redux documentation, it seems like there is always a return value from the middlewares. However, in my experience, simply calling next(action) and not returning anything seems to work without any issues. ...

I desire to activate the textbox only when the radiobtnlist value equals 1

I am trying to disable a textbox based on the selected value of a RadioButtonList in my code. However, the functionality is not working as expected and I am unsure why. <script type="text/javascript"> $(function () { $("#RadioButton ...

Attempting to compare the HTML datetime with the current time in order to implement conditional formatting

I am working with a SharePoint discussion list that includes a field named "Last Updated." My goal is to identify and highlight rows where the last update was more than 1 hour ago. Here is my current approach: <html> <head> <sc ...

Preventing blank text entries in a task management application

Is there a way to determine if the text input provided by the user is empty or contains some text? I'm in the process of developing a TO-DO app and I'd like it so that if a user fails to enter anything into the text area and clicks on "add", the ...

The array containing JSON objects enclosed within curly braces is causing a syntax error

Given a variable containing data that looks like an "array" with JSON Objects inside (even though it is not actually formatted as an array, starting and ending with curly braces): {"x1","x2"},{"y1","y2"},{"z1","z2"} How can I transform this so that the i ...

Are Viewmodel contents empty after ajax request?

Currently working on an ASP.NET MVC application, I am in the process of developing a search page that showcases both the search box and the table of results simultaneously. To achieve this functionality, I have utilized Partial Views along with AJAX/JSON c ...

The v-model in Vue does not function correctly when used with a single index within an array

I am encountering a situation with a series of input fields, including a checkbox and a dropdown button in each row. Additionally, there is a button to add a new row of inputs. When the checkbox is checked, it should disable the dropdown menu and make it ...

Problem encountered with updating the content data in Handsontable

I'm encountering an issue while trying to implement the handsontable. Specifically, I need to re-render the handsontable based on a dropdown selection. However, despite my efforts, the table does not update correctly after selecting a value from the d ...

What is the best method to ensure that none of the select option values are left empty?

I have a total of 5 select option drop down menus currently, but this number may increase in the future depending on requirements. The issue I'm facing is that when I select the last element, it returns true even though the other elements are empty. I ...

AngularJS postback method fails to trigger

Seeking assistance with my angularJS AJAX postback method. Below is the HTML code I have created: <html xmlns="http://www.w3.org/1999/xhtml" ng-app> <head runat="server"> <title></title> <script src="Scripts/angular.js ...

Tips on updating content at a specific time without the need to refresh the page

I'm working on a digital signage script that needs to be time scheduled. I was able to accomplish this using JavaScript and refreshing the page every 15 minutes. However, my question is, how can I track the time and update the content at specific hour ...

Encountering Type Error in Angular 2

Here is the Angular 2 code snippet I am working with: <ion-grid *ngFor="let item of menuData; let i = index;" ng-init="getAllItemToGrid()"> <img src="{{'assets/Products/'+menuData[i].image}}" ng-click="onLogin()" width="100%"> ...

Identifying the specific promise that failed within a chain of .then statements

I am currently working on setting up a chain of promises with an error catch at the end within my node and express application. One issue I have encountered is that if any of the 'then' functions encounter an error, it can be difficult to trace b ...

Angular Checkbox Single Select

I have a piece of HTML code with ng-repeat that includes checkboxes. <table> <tr ng-repeat="address in contactInfo.Addresses"> <td>{{address.DisplayType}}</td> <td>{{address.AddressLine1}}</td> <td>{ ...