小编Lhe*_*hew的帖子

typescript 上的 document.querySelectorAll:类型“NodeListOf<HTMLElement>”不是数组 type.ts(2461)

我一直在尝试一种方法来在打字稿上获得以下内容:

    [...document.querySelectorAll<HTMLElement>("#node-a input, #node-a button")].forEach(item => {
//code
    })

Run Code Online (Sandbox Code Playgroud)

但我收到以下错误:

Type 'NodeListOf<HTMLElement>' is not an array type.ts(2461)

这有什么问题吗?

dom typescript

4
推荐指数
1
解决办法
6338
查看次数

暗模式在重新加载时闪烁白色背景一毫秒

我正在尝试在我的应用程序中添加此暗模式功能。它使用 localstorage 来存储用户的偏好以供将来使用。所以现在的问题是当启用了暗模式,并且由于某种原因重新加载页面时,同样是用户故意重新加载页面或提交表单,然后页面上到处都是白色背景闪烁,然后变成黑暗的。它停留在几分之一秒。只是看起来不专业。

还没有找到任何解决办法。所以请帮帮我。

附注。下面的代码片段在 SO 中不起作用,因为代码包含localStorage对象。

这是代码:

const toggleSwitch = document.querySelector('#dark-mode-button input[type="checkbox"]');
const currentTheme = localStorage.getItem('theme');

if (currentTheme) {
    document.documentElement.setAttribute('data-theme', currentTheme);
    if (currentTheme === 'dark') {
            toggleSwitch.checked = true;
    }
}

function switchTheme(e) {
    if (e.target.checked) {
        document.documentElement.setAttribute('data-theme', 'dark');
        localStorage.setItem('theme', 'dark');
    }else {        
        document.documentElement.setAttribute('data-theme', 'light');
        localStorage.setItem('theme', 'light');
    }    
}

toggleSwitch.addEventListener('change', switchTheme, false);  
Run Code Online (Sandbox Code Playgroud)
:root {
  --primary-color: #495057;
  --bg-color-primary: #F5F5F5;
}

body{
  background-color: var(--bg-color-primary); 
}

[data-theme="dark"] {
  --primary-color: #8899A6;
  --bg-color-primary: #15202B;
}

table {
  font-family: arial, sans-serif;
  border-collapse: collapse; …
Run Code Online (Sandbox Code Playgroud)

html javascript css

4
推荐指数
1
解决办法
1054
查看次数

HtmlWebpackPlugin - ERROR in Error: The loader "[...]/html-webpack-plugin - /lib/loader.js!/[...]/src/index.html" didn't return html

I'm trying to load an HTML template using HtmlWebpackPlugin and it seems not working. If I try to load the plugin without arguments it works. Any ideas?

The index.html file that I'm trying to load is in the right place, just to consider

package.json:

{
 ...
  "devDependencies": {
    "@babel/core": "^7.7.4",
    "@storybook/html": "^5.2.8",
    "babel-loader": "^8.0.6",
    "babel-plugin-syntax-dynamic-import": "^6.18.0",
    "css-loader": "^3.2.1",
    "html-webpack-plugin": "^3.2.0",
    "mini-css-extract-plugin": "^0.8.0",
    "node-sass": "^4.13.0",
    "sass-loader": "^8.0.0",
    "style-loader": "^1.0.1",
    "ts-loader": "^6.2.1",
    "typescript": "^3.7.3",
    "webpack": "^4.41.2",
    "webpack-cli": "^3.3.10",
    "webpack-dev-server": "^3.9.0"
  },
  "dependencies": …
Run Code Online (Sandbox Code Playgroud)

javascript build webpack

3
推荐指数
2
解决办法
6536
查看次数

打字稿的自动完成在 VSCode 上显示错误的路径

在自动完成方面,我在 typescript + VSCode 上遇到了一些烦人的问题:

每当我尝试自动完成时,它永远不会带来正确的路径。如果我在./src/components/foo并且我输入Bar从 './src/components/bar/index.tsx' 获取它,而不是自动完成完成import {Bar} from '../bar',我总是得到import {Bar} from 'src/components/bar'

我的tsconfig.json

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "noImplicitAny": false,
    "resolveJsonModule": true,
    "sourceMap": true,
    "module": "commonjs",
    "target": "es5",
    "jsx": "react",
    "rootDirs": ["src", "stories"],
    "allowJs": true,
    "baseUrl": ".",
    "paths": {
      "my-lib": ["./node_modules/my-lib/dist/*"]
    }
  },

  "include": [
    "index.d.ts",
    "./src/**/*",
    "./stories/**/**/*"
  ]
}
Run Code Online (Sandbox Code Playgroud)

typescript reactjs webpack visual-studio-code

2
推荐指数
1
解决办法
2551
查看次数

标签 统计

javascript ×2

typescript ×2

webpack ×2

build ×1

css ×1

dom ×1

html ×1

reactjs ×1

visual-studio-code ×1