【问题标题】:Button onClick using .jade with a JS function使用 .jade 和 JS 函数的按钮 onClick
【发布时间】:2020-07-05 02:01:33
【问题描述】:

我正在尝试对我的 .jade 文件中的按钮 onClick 使用函数。该函数位于 javascript 文件中。每当单击按钮时,我都想使用屏幕截图功能。我的问题是我是否正确导入了 .js 文件?另外,我是否正确调用了该函数?

当我运行时,这是我得到的错误:

Invalid or unexpected token on the line button(onClick = # 
{screenshots(client)}) GO

索引.jade

extends layout

block content
  h1= title
  script (src='../scripts/screenshot.js')
  p Welcome to #{title}
  button(onClick = #{screenshots(client)}) GO
  div.Windows
    img(src="http://localhost:3000/images/myscreenshot2.png", class="image", width="20%", height="20%")  
    object(data="http://localhost:3000/test.txt",class = "texttest",width='50%', height='615px' display: block;)

screenshot.js

async function screenshots(client) {
    await client.takeScreenshot().then(
client.saveScreenshot("./public/images/myscreenshot2.png")
);
}


async function connection () {

    const wdio = require("webdriverio");
    const assert = require("assert");

const opts = {
    port: 4723,
    capabilities: {
        platformName: "Android",
        platformVersion: '8.1',
        deviceName: "emulator-5554",
        app: "C:\\Users\\user1\\Downloads\\test.apk",
        automationName: "UiAutomator2"
    }
};
    const fs = require('fs');   
    const client = await wdio.remote(opts);

【问题讨论】:

    标签: javascript pug


    【解决方案1】:

    您的问题来自混合服务器端 pug/jade 和客户端 JavaScript。

    让我们专注于您的按钮代码:

    button(onClick = #{screenshots(client)}) GO
    

    #{} 是一条 pug 指令,用于将变量的值插入该空间。在按下按钮时,您只希望它调用本地脚本,并且不需要 pug/jade:

    button(onClick= "screenshots(client)") GO
    

    您得到的错误代码是由于试图调用一个函数,其中 pug/jade 期望一个可以计算为字符串的表达式。

    我无法确定,因为我不知道您的文件系统是如何设置的,但您可能需要使用绝对路径引用脚本:

    script (src='/scripts/screenshot.js')
    

    【讨论】:

    • 感谢您的澄清。现在我将按钮更改为 button(onClick = "screenshots(client)") Go 我收到错误:未定义屏幕截图。这是正确的导入方式吗?脚本(src='../scripts/screenshot.js')
    • 您是否查看了浏览器中的网络选项卡以查看该文件是否已正确加载?那应该告诉你发生了什么。我只是在编辑答案时给了你一个提示。
    • 我的公开文件中没有该文件,因此找不到它。感谢您的帮助!
    猜你喜欢
    • 2015-02-24
    • 2020-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-14
    • 2019-11-06
    • 2021-11-30
    • 2021-01-12
    相关资源
    最近更新 更多