vscode自定义片段
1、Code -> 首选项 -> 用户片段
2、填入代码片段文件的名称,然后回车
3、在打开发文件中,写入代码片段的配置,如:
{ // Place your 全局 snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and // description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope // is left empty or omitted, the snippet gets applied to all languages. The prefix is what is // used to trigger the snippet and the body will be expanded and inserted. Possible variables are: // $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. // Placeholders with the same ids are connected. // Example: "react_s_page": { "scope": "javascript,typescript,javascriptreact,typescriptreact", "prefix": "react_s_page", "body": [ "/* 三方库 */", "import React from 'react'", "import { Link } from 'react-router-dom'", "", "/* redux 链接组件 */", "import concatRedux from '@utils/concatRedux'", "", "/* 引入 ts 公共 interface定义 */", "import {IConnectProps} from '@tsDefine/ICommon'", "", "/* 引入公共组件 */", "import Page from '@components/Page'", "", "/* 相对路径引入组件 */", "", "/* 引入样式 */", "import './index.scss'", "", "/* ts定义interface */", "interface IProps extends IConnectProps { }", "interface IState {}", "", "/* concatRedux 定下定义变量,避免js 的 @装饰器被 ts 检测然后报错 */", "const conncatRedux: Function = concatRedux;", "", "@conncatRedux('/$1', ['$2'], ['$3'])", "export default class Center extends React.Component<IProps, IState> {", " componentDidMond(){", "", " }", " render() {", " return (", " <Page title='$4' className='page_$5'>", "", " </Page>", " )", " }", "}", ], "description": "react 页面框架" } }
4、然后就可以再定义的类型的文件中使用该代码片段,如我在 scope 中定义了 javascript,typescript,javascriptreact,typescriptreact 这四种类型的文件中可以使用 react_s_page 前缀插入该代码片段。
官方文档:https://code.visualstudio.com/docs/editor/userdefinedsnippets