Is there a way to combine Vue3, Stripe, and Typescript for seamless integration?

I am currently developing a Vue3 application and running into some issues while trying to integrate Stripe. I am facing difficulty in incorporating it successfully.

Here is the code snippet from my Vue3 component named Checkout.vue:

<template>
....
...
</template>
<script lang="ts" setup>

import { onMounted, ref, reactive, computed} from 'vue';
import { loadStripe } from '@stripe/stripe-js'
import { useShoppingCart } from '@/stores/shoppingcart'
import axios from 'axios'
import { useRouter } from 'vue-router';

const shoppingcart = useShoppingCart()
const router = useRouter()
const stripe = reactive({})
const cardElement = reactive({})
const customer = reactive({
                    first_name: '',
                    last_name: ''
                    ...                    
                    ...
                })
const paymentProcessing = ref(false)

onMounted(async () => {
    stripe = await loadStripe(import.meta.env.VITE_MIX_STRIPE_KEY)        

    let elements = stripe.elements();

    cardElement = elements.create('card', {
        classes: {
            base: 'bg-gray-100 rounded border border-gray-300 focus:border-indigo-500 text-base outline-none text-gray-700 p-3 leading-8 transition-colors duration-200 ease-in-out'
        }
    });

    cardElement.mount('#card-element');

});

const processPayment = async () => {

    paymentProcessing.value = true;

    const {paymentMethod, error} = await stripe.createPaymentMethod(...)
 ....

}

The issue persists with an error message stating:

Cannot assign to 'stripe' because it is a constant.

Additionally, the credit card input for Stripe within the Vue component fails to load. It's imperative for me to leverage Stripe not only within onMounted hooks but also within the processPayment method. Any suggestions on how to resolve this would be greatly appreciated. Thank you.

Answer №1

The reason you cannot assign a value to a const variable is because it is meant to be constant.

Consider using a let variable instead for this purpose.

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

Ways to verify if the current date exists within a TypeScript date array

I am trying to find a way in typescript to check if the current date is included in a given array of dates. However, even after using the code below, it still returns false even when the current date should be present within the array. Can anyone please pr ...

Limit file upload size to less than 1MB in Angular 2 with typescript using ng2-file-upload

Having issue with my code - I can't upload a file larger than 1mb even though maxFileSize is set to 50mb. Can anyone help me troubleshoot? @Component({ moduleId: module.id, selector: 'NeedAnalysisConsult', templateUrl: 'nee ...

What are the different ways to customize the appearance of embedded Power BI reports?

Recently, I developed a website that integrates PowerBI embedded features. For the mobile version of the site, I am working on adjusting the layout to center the reports with a margin-left style. Below are the configuration parameters I have set up: set ...

What steps do I need to take to set up Vue-CLI 3 to generate a webpage with no JavaScript?

My dilemma is this: I have a simple static page that does not require JavaScript. Using vue-cli 3, I am attempting to pass the HTML file through webpack for minification purposes. Unfortunately, it seems that accomplishing this task is not as straightforwa ...

Reusing a lazy-loaded module across multiple applications

Currently, I am working on an enterprise Angular 2 application with numerous lazy loaded modules. A new project came up where I needed to reuse a module that was previously created for the main app. After researching online, the only solution I found was ...

Creating a JSON object from an array of data using TypeScript

This might not be the most popular question, but I'm asking for educational purposes... Here is my current setup: data = {COLUMN1: "DATA1", COLUMN2: "DATA2", COLUMN3: "DATA3", ..., COLUMNn: "DATAn"}; keyColumns = ["COLUMN2", "COLUMN5", "COLUMN9"]; ...

Nuxt: Issue with Head function affecting title and meta tags

I need to customize the titles and meta descriptions of different pages on my Nuxt website. However, I am struggling to make it work using the head() method as indicated in the documentation. For example, in my contact.vue file: export default class Cont ...

Leveraging Vue.js for implementing reusable form validation rules

It seems like I'm repeating a lot of code within my form components. Each form component follows this structure: template <v-col cols="12" sm="6" md="4"> <v-text-field name="name" v-model="user.name" ...

Vue.js dynamic HTML elements are constantly changing and evolving

Is it possible to dynamically add elements to the content? See example below: <template> {{{ message | hashTags }}} </template> <script> export default { ... filters: { hashTags: function(value) { ...

What is the best method for releasing an NX library along with all its bundled dependencies?

This problem is quite common in GitHub's NX repository, but I have not been able to find a solution there. Within my workspace, I have two buildable libraries: ui/avatar and ui/icon, as well as a publishable library named bar The goal is to utilize ...

Injecting variable styles into my VueJS component

I am currently working on developing a custom progress bar to visualize the percentage of completed tasks. The approach I am taking involves using v-bind:styles and passing {width: dynamicWidth + '%'} to regulate the progression of the bar. To ac ...

Utilize the clearChart() function within Google charts in conjunction with vue-google-charts

I have integrated vue-google-charts to display various charts on my website. I want to allow users to compare different data sets, by enabling them to add or delete data from the chart. In order to achieve this functionality, I need to find a way to clear ...

Tips for avoiding multiple reference paths in Angular TypeScript: - Simplify your imports

Setting up Typescript for an Angular 1.5 application has been a bit of a challenge. To ensure that a TS file can be compiled by gulp without any errors, I have to include the following line: ///<reference path="../../../../typings/angularjs/angular.d.t ...

What is the proper way to utilize the script type "text/x-template" in a multilanguage context?

I'm currently working with ASP NET and VueJS. In my ASP NET, I have a view component that looks like this: <script type="text/x-template" id="form-template"> @if (string.IsNullOrEmpty(Model.LastName)) { <in ...

Vue and Vuex: importing a temporary collection of child components

In our vue/vuex application, we are using an object called myObject which contains a group of children referred to as myObject.kids. When the object is initially loaded by vuex, the .kids data is not included. Now I am looking to create a component that ...

Exploring the attributes of optional features

Dealing with optional properties can be quite tedious. Consider the object test1 in TypeScript: interface Test { a?: { b?: { c?: { d?: string } } }; } const test1: Test = { a: { b: { c: { d: 'e' } } } }; Handling the absence of each proper ...

How can one retrieve information from within a vue.js function?

As a newcomer to VueJS, I am seeking help on updating data upon click. Below is my code snippet: <div id="elementApp"> <div class="element">{{ message }}</div> <button class="button" v-on:click="nextElement()">Next data</but ...

Tips for ensuring that the cursor is directed to the initial input when using a Vue template form generator

If you're looking for an example, check out the readme of this page: https://github.com/vue-generators/vue-form-generator My goal is to focus the cursor on the first input field in a Vue form generator, specifically the "Name" field. I've atte ...

``Engulfed in a sea of NgRx ViewModel

Apologies if this is unclear, there might be a fundamental aspect that I am overlooking, but here is my current issue: I am fetching a list of items from the backend, similar to: interface Item { id: number; userId: number; categoryId: number; } ...

What is the best method for transitioning to a new page in React Native using Ignite Bowser?

Recently I ventured into the world of React Native with Ignite Bowser. My current project involves building a React Native app using Ignite Bowser. At the start of my app, there's a welcoming screen that pops up. It features a 'continue' bu ...