What is the best way to uninstall the Google Translation Plugin when transitioning to a new Vue component?

I'm facing a minor issue as I develop a website builder. It consists of two main parts: the builder and the preview section. Within the preview, I've included the Google Translation plugin:

onMounted(() => {
  new google.translate.TranslateElement({}, 'google_translate_element');
})

I have placed the script in index.html, just before closing <head>:
        <script
                type="text/javascript"
                src="https://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"
        ></script>

However, when I switch back to the builder from the preview and then return to the preview, my Google Translate plugin fails to load (although onMounted is triggered). The console doesn't show any errors. First load: https://i.stack.imgur.com/VsNqA.png Second load: https://i.stack.imgur.com/GDVws.png I attempted to remove the TranslateElement upon unmounting, but that didn't resolve the issue. Are there any other solutions I could explore? Thanks!

Answer №1

I implemented the solution below and it was successful.

To ensure proper functionality, it is important to place the google translator script before closing the </body> tag. This is crucial because if the script, which is located in the </head>, experiences delays in loading (beyond your control), it could halt the entire page loading process until it finishes loading.

Your HTML structure should resemble the following:

<!DOCTYPE html>
<html lang="en">
  <head>
    ...
  </head>
  <body>
    <div id="app"></div>
    <div id="google_translate_element"></div>

    <script type="module" src="/src/main.js"></script>

    <script
      type="text/javascript"
      src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"
    ></script>
  </body>
</html>

In your main.js or App.vue file, ensure you have this specific function and call it accordingly. I utilized the mounted hook within App.vue as shown below:

{
mounted () {
  this.googleTranslateElementInit()
},
methods: {
  googleTranslateElementInit () {
    window.google.translate.TranslateElement(
      { pageLanguage: "en" },
      "google_translate_element"
    );
  }
}

This approach has been tested on both Vue2 and Vue3 frameworks.

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

The Selenium test functions correctly in the production environment, however it encounters issues when run

I am facing an issue with my Vue.js application while writing Selenium tests. The test passes when run against the deployed production version of the app, but fails when run against a local version. When running the app locally, it is not from a build, bu ...

Executing a python script on the client side with the help of Vue.js

Because of restricted computation capabilities, I need to execute a python script on the user's browser. Currently, my website utilizes Vue.js for the frontend and Django for the backend. Do you have any suggestions on how I can specifically run thi ...

Solving the CORS problem between Vue.js and Flask: Troubleshooting XMLHttpRequest Blockade

Description: Currently working on developing a web application utilizing Vue.js for the frontend and Flask for the backend. The initial phase involves creating a simple login page, but encountering CORS (Cross-Origin Resource Sharing) issues when making r ...

Django Vue3 encounters access-control-allow-origin restriction

I am currently working on a Django rest-api project that uses Vue on the front-end. Unfortunately, I encountered an error while making requests via Vue: Console output: The following error is displayed: Access to XMLHttpRequest at 'https://api.iyziw ...

jinja2.exceptions.UndefinedError: The variable 'asset' has not been defined

Currently in my project, I am using a Python backend to fetch data from an API and then rendering it through Flask to the Vue.js frontend. However, I have encountered an error titled that is causing some issues. I have double-checked and printed the varia ...

What is the most efficient method for transferring Flask variables to Vue?

I am currently developing a visualization application using a flask server and vue.js for the front end. Other discussions on this topic explore how to avoid conflicts between vue.js and flask variable syntax, as shown here. In my scenario, I'm inte ...

Flask static serving causes Vue app to not render

Currently, I am in the process of developing a basic web application utilizing Flask for the backend and Vue for the client side. Here is an overview of my folder structure: . ├── client/ │ ├── dist/ │ │ ├── css/ │ │ ...

An error occurred: gyp ERR! stack - The command `python -c import sys; print "%s.%s.%s" % sys.version_info[:3]` has failed

While attempting to npm install in a Vue project, I encountered an error even after running vue create (name): npm ERR! gyp verb check python checking for Python executable "c:\Python310\python.exe" in the PATH npm ERR! gyp verb `which` ...