Guide on utilizing raw HTML files within Nuxt (Vue.js) framework

I am looking to set up a page that redirects to the stripe payment page using stripe-samples/firebase-subscription-payments. I have placed its html/css/js files (found in the "public" folder) in my nuxt app's /static directory. However, since this is for static files only, I cannot utilize vuex and plugins. Luckily, the script tag in html can fetch firebase from the URL, allowing me to incorporate firebase.

Is it possible to directly add raw html/css/js files to nuxt/pages like .vue files? I attempted this but faced challenges. While the ideal solution would be to convert the html/js file into a vue file, as a beginner it proved to be too complex for me. Additionally, English is not my native language, so apologies for any errors. Can npm packages and modules be used in /static files? I have spent two days researching this without finding a solution. Any assistance would be greatly appreciated, thank you!

Here is an excerpt of my code: static/public/javascript/app.js


    import firebase from firebase; 
    /*↑ it will be error "Cannot use import statement outside a module".
    In pages/.vue files and plugins/files, however, it works. 
    I also attempted "import firebase from '~/plugins/firebase.js'";*/
    
    const STRIPE_PUBLISHABLE_KEY = ....

static/public/index.html

    <!-- Firebase App (the core Firebase SDK) is always required and must be listed first -->
    <script src="https://www.gstatic.com/firebasejs/7.14.6/firebase.js"></script>
    <script src="https://www.gstatic.com/firebasejs/7.14.6/firebase-functions.js"></script>

    <!-- If you enabled Analytics in your project, add the Firebase SDK for Analytics -->
    <script src="https://www.gstatic.com/firebasejs/7.14.5/firebase-analytics.js"></script>

    <!-- Add Firebase products that you want to use -->
    <script src="https://www.gstatic.com/firebasejs/7.14.5/firebase-auth.js"></script>
    <script src="https://www.gstatic.com/firebasejs/7.14.5/firebase-firestore.js"></script>

↑ It fetches firebase from the URL, but I desire to employ the firebase module that I have installed.

Answer №1

After some deliberation, I decided to rework the JavaScript/HTML code into Vue. The process involved transferring the existing JS code to the mounted() hook, but encountered challenges in manipulating the nested template tag with JS. As a solution, I incorporated v-if and v-for directives to achieve the desired functionality.

Answer №2

There are multiple solutions available for this issue.

If you need to load 3rd party scripts, one option is to refer to this solution:

Another approach could be utilizing Nuxt plugins depending on the requirements: https://nuxtjs.org/docs/2.x/directory-structure/plugins/

Additionally, there's a solution available for properly integrating Stripe in Nuxt:

Remember that the static directory is not meant for storing your code. It should only contain public assets like favicons or shared PDF files.

While I haven't personally worked with firebase and Nuxt recently, these resources might still offer guidance on implementing it effectively in Nuxt.

Answer №3

One way to load the html is by using the render() method in your Vue component file.

Check nuxt.config.js for this configuration:

    build: {
        extend(config, ctx){
            config.resolve.alias['vue'] = 'vue/dist/vue.common';
        }
    }

In your Vue component file, you can use the following code:

<script>
import myHtml from '~/path/to/your/html/my_html.html';
export default{
    render(h){
         return h({
              template: `<main>${myHtml}</main>`
         });
    }
}
</script>

Once done, your page will be rendered as a component in either your Vue <router-view/> or <nuxt/>. Just ensure that there are no <script> tags within your my_html.html file. Instead, place any JavaScript code inside the render() method like shown below:

<script>
import myHtml from '~/path/to/your/html/my_html.html';
export default{
    render(h){
         return h({
              template: `<main>${myHtml}</main>`,
              created(){
                   console.log('I have been created!')
              },
              mounted(){
                   console.log('I have been mounted!')
              },
              methods: {
                   exampleMethod(){
                        alert('this is an example')
                   }
              }
         });
    }
}
</script>

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

Why must I handle Access-Control-Allow-Origin issues with XMLHttpRequest() but not with forms?

When attempting to make a request using XMLHttpRequest(), I encounter the Access-Control-Allow-Origin error. However, when I use a form like the one below, no issues arise. Is there something about forms that allows access? What could be the reason for t ...

What is the best way to reset an animation back to its original state after clicking the button again?

Forgive my lack of understanding, but I can't seem to figure this out. I have a navigation bar centered both horizontally and vertically with a button inside. When the button is clicked, the nav bar animates to the top of the page with a slight margin ...

What strategies can be employed to reduce the need for numerous if-else statements in my JavaScript code?

In my input form, users have the ability to make edits. I need to send only the data that has been changed by the user. Currently, I am repeating the same lines of code multiple times to compare and determine what data needs to be sent. Is there a more e ...

Styling Your Navigation Bar with CSS and Active States

Creating an interactive navigation element for a menu can be challenging, but here's a helpful example. http://jsfiddle.net/6nEB6/38/ <ul> <li><a href="" title="Home">Home</a></li> <li class="activ ...

Combining two variables in AngularJS seamlessly, without explicit instruction

In the realm of Controllers resides my beloved home.js: angular.module("HomeApp", ["BaseApp"]) .controller("MainCtrl", ["$http", "$window", "BaseService", function($http, $window, BaseService) { var self = this; self.posts = BaseServ ...

What is the best way to detect component errors on the server with Angular Universal?

Here is a snippet of my server code that renders the Angular home.component: app.get("*", (req, res) => { res.render( `../${CLIENT_DIST_DIR}/index`, { req: req, res: res, providers: [ ...

The tabs are visible in the drawer, but the transition is not occurring

I am a beginner when it comes to using material ui. After clicking on advanced sports search, a drawer pops up. I have been trying to incorporate tabs in the drawer, but every time I click on tab two, the drawer closes. In reality, it should switch between ...

JQuery Anchor Link Scrolling Problem on Specific Devices

I'm having an issue where my webpage does not scroll correctly to an anchor when a link is clicked. The problem arises from the header size changing based on the viewport width. While it works fine in desktop Chrome, in the mobile version the header h ...

Single-node interaction with three.js

In this demonstration, you can observe two clickable particles that both respond to clicks. I am seeking a way to detect clicks on particles without removing them from the scene, similar to this: if (intersects.length>0){ if(intersects[0].object.ty ...

Encountering Build Issue: "NgSemanticModule is not recognized as an NgModule" persists despite inclusion of dependencies and importing into primary module

I have posted my module, component, and package file here. I am attempting to implement a click event with ngif, but I keep encountering an error. The specific error message is "ERROR in NgSemanticModule is not an NgModule". I'm unsure if this error ...

Press the button obscured by an image

Recently on my HTML website, I encountered an issue where I tried adding a transparent image over a button but found that I was unable to click on the button afterwards. The problem seems to be related to the following code snippet: #container { heigh ...

Rearrange two HTML tags that are separated by certain fixed tags

I currently have a Div tag with 3 Elements (HTML Tags) inside. The first tag is: field set, The second tag is: div with a fixed position and multiple inner tags, The third tag is: field set. After performing certain conditions in the document ready eve ...

Ways to incorporate lighting into the instancing shader

Is there a way to incorporate ambient and directional lighting into the shader when using InstancedBufferGeometry? For example, I am looking to add lighting effects to a specific instance, like in this example: Below is the vertex shader code: precision ...

Retrieving specific values from a JArray containing Lists of JInts

Currently, I am honing my skills in Scala and Akka Streams while working with the Stripe payment API. My focus is on extracting the transaction date labeled created from the Json response. Here's an example of the response (displaying 2 transactions) ...

Ensure that you select the checkbox if it is visible; if it is not, then you should

I am currently working on a page script and I only require a simple JavaScript solution. When a specific text, for example - no found, appears on the page, the script should automatically click the find work button. This action needs to be triggered at 1 ...

Show the text of a link when it is selected using the tab key in a JavaScript event

Snippet: <a href="#about"> About <abbr title="The Little Taco Shop">About LTS</abbr></a> Show the text "About LTS" when the tab key is pressed on the keyboard. ...

How to access a webpage on your Android device without the address bar visible

My question relates to sending Push Notifications through OneSignal. Upon clicking a push notification, it automatically redirects the user to my website. Is there a way to hide the address bar on the browser when this happens? I attempted using the follo ...

Exploring date formatting in NestJs with Javascript

Currently, I am working with a ScrapeResult mikroOrm entity. I have implemented the code newScrapeResult.date = new Date() to create a new Date object, resulting in the output 2022-07-17T17:07:24.494Z. However, I require the date in the format yyyy-mm-dd ...

update the componentWillMount() function

I am currently exploring the code found at https://github.com/Spyna/react-store/blob/master/src/createStore.js What adjustments do I need to make in order to align with the deprecated componentWillMount lifecycle method? CreateStore const createStore = ...

Thinking about reconfiguring the component's API?

How do I implement this with the composition API in Vue 3? checkboxFilter:{genre: [], moods: [], tempo: [], theme: []}, computed: { filteredSongs: function(value, key){ let fltrdSongs = this.songs; if(this.checkboxFilter.genre.length + this.checkboxFilter ...