我一直在尝试一种方法来在打字稿上获得以下内容:
[...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)
这有什么问题吗?
我正在尝试在我的应用程序中添加此暗模式功能。它使用 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)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) 在自动完成方面,我在 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)