Heim  >  Fragen und Antworten  >  Hauptteil

Verwenden Sie „secondary.html“, um eine Anwendung in Vue3 dynamisch zu starten

Ich entwickle ein vue3-Projekt.

main.js;

import { createApp } from "vue";
import App from "./App.vue";
const app = createApp(App);
import store from "./store";
app.use(store);
import router from "./router/index";
app.use(router);
...

//just try...
app.mount("#app");

und meine public/index.html

<!DOCTYPE html>
<html lang="">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <link rel="icon" href="<%= BASE_URL %>favicon.ico">
    <title><%= htmlWebpackPlugin.options.title %></title>

    <style>
        body {
            margin: 0px;
        }
    </style>
</head>
<body>
    <noscript>
        <strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
    </noscript>
    <div id="app"></div>
    <!-- built files will be auto injected -->
</body>
</html>

Und mein App.vue;

<template>
        <button @click=openNewPage()>create new page</button>
        <span>{{message}}</span>
        <router-view />
    </template>
    <script>
        methods:{
            openNewPage(){
                var t = window.open('second.html','newMonitor','height=700,width=700,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes');
    
            }
        }
    </script>

Mein Ladenobjekt;

export default createStore({
    state: {
      message:'Hello'
    }
});

und meine zweite .html;

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="Cache-Control" content="no-store" />
    <meta http-equiv="Pragma" content="no-cache" />
    <meta http-equiv="Expires" content="0" />
    <title></title>
</head>
<body>
    <div id="appSecond" style="height:100%">
        <template>
            <span>{{message}}</span>
        </template>
    </div>
</body>
</html>

Wenn ich den zweiten Bildschirm mit der OpenNewPage-Methode öffne, kann ich nicht auf das Store-Objekt zugreifen und die Komponente, die ich verwenden möchte, funktioniert nicht. Ich versuche es;

Second.js

const app2 = createApp({
});

export { app2 };

und meine Methode

import { app2 } from "../second.js";
openNewPage(){
    var t = window.open('second.html','newMonitor','height=700,width=700,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes');
    
    if (t) {
        t.onload = function () {
        t.window.app = app2;
        app2.mount("#appSecond");
        }
    }
}

Irgendwie habe ich versucht, die Datei „secondary.html“ auszuführen, bekam aber eine Warnung wie „[Vue-Warnung]: Anwendung konnte nicht installiert werden: Zielselektor wird installiert“. Der Code funktioniert jedenfalls nicht. Kannst du mir helfen?

P粉354948724P粉354948724203 Tage vor339

Antworte allen(1)Ich werde antworten

  • P粉514001887

    P粉5140018872024-03-30 09:34:57

    openNewPage() 无法为新打开的页面运行脚本;只有新页面可以运行自己的脚本。当 openNewPage() 尝试 app2.mount() 时,它在自己的页面(而不是新页面)上运行该代码,从而导致您观察到的错误。

    解决方案

    删除openNewPage()中的挂载代码,使其仅打开新页面:

    export default {
      openNewPage() {
        window.open('second.html', …);
      }
    }
    

    更新second.html以加载挂载应用的脚本,并删除<div id="appSecond">中不必要的<template>

    
      
    {{message}}

    second.js 中,添加安装您的应用程序的代码:

    // second.js
    import { createApp } from 'vue'
    import App from './App.vue'
    import store from './store'
    
    createApp(App).use(store).mount('#appSecond')
    

    Antwort
    0
  • StornierenAntwort