GoogleChromeLabs / carlo

Web rendering surface for Node applications
Apache License 2.0
9.31k stars 309 forks source link

How to use exposed function from Vue Single File Components? #36

Closed PikachuHy closed 6 years ago

PikachuHy commented 6 years ago

demo main.js (expose function homepath)

'use strict';

const carlo = require('carlo');
const os = require('os');
const path = require('path');

(async () => {
    const app = await carlo.launch(
        {
            width: 1000,
            height: 500,
            userDataDir: path.join(os.homedir(), '.carlosysinfo'),
            args: process.env.DEV === 'true' ? ['--auto-open-devtools-for-tabs'] : []
        });
    app.on('exit', () => process.exit());
    app.serveFolder(__dirname + '/dist');
    await app.exposeFunction('homepath', homepath);
    await app.load('index.html');
})();
function homepath() {
    return os.homedir()
}

main.vue (Vue Single File Components)

<template>
  <div id="app">
    {{ path }}
  </div>
</template>

<script>
    export default {
        name: 'app',
        components: {},
        data() {
            return {
                path: ''
            }
        },
        async mounted () {
            // ???? How to 
            this.path = homepath()
        }
    }
</script>

it not work

Uncaught (in promise) ReferenceError: homepath is not defined
djyde commented 6 years ago

use window.homepath()

nickbouldien commented 6 years ago

nice, I'm messing around with something similar but with react. window. was what I used as djyde mentioned

https://github.com/nickbouldien/carlos

PikachuHy commented 6 years ago

it work. thanks